Adios Means Goodbye – Browser 301 Redirects In All Languages

By  on  

Browser redirects, especially 301 "permanent" redirects, are essential to all good web applications. Regardless of language, browser redirects can:

  • provide safe URL forwarding to gather GET and POST variables and process them without risking data and processing integrity by a browser refresh
  • send users and search engine bots to the new location of a page or entire website
  • maintain search engine rank and avoid 404 errors

Here's the list of browser redirects using various languages:

.htaccess

redirect 301 / http://www.davidwalsh.name/

ASP

Response.Status="301 Moved Permanently";
Response.AddHeader("Location","http://www.davidwalsh.name/");

ASP.NET

ColdFusion

<CFHEADER statuscode="301" statustext="Moved Permanently">
<CFHEADERname="Location" value="http://www.davidwalsh.name">

Javascript (NOT a 301)

//window.location.href = 'http://www.davidwalsh.name/';

Java JSP

response.setStatus(301);
response.setHeader("Location", "http://www.davidwalsh.name/");
response.setHeader("Connection", "close");

Meta tag (NOT a 301)

<meta http-equiv="refresh" content="0;url=http://www.davidwalsh.name/" />

Perl

use strict;
print "Status: 301 Moved Permanantlyn";
print "Location: http://www.davidwalsh.name";
exit;

PHP

header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.davidwalsh.name');

Ruby On Rails

def old_action
headers["Status"] = "301 Moved Permanently"
redirect_to "http://www.davidwalsh.name/"
end

Do you have any more redirect scripts? If so, post them below!

Recent Features

  • By
    Interview with a Pornhub Web Developer

    Regardless of your stance on pornography, it would be impossible to deny the massive impact the adult website industry has had on pushing the web forward. From pushing the browser's video limits to pushing ads through WebSocket so ad blockers don't detect them, you have...

  • By
    5 HTML5 APIs You Didn&#8217;t Know Existed

    When you say or read "HTML5", you half expect exotic dancers and unicorns to walk into the room to the tune of "I'm Sexy and I Know It."  Can you blame us though?  We watched the fundamental APIs stagnate for so long that a basic feature...

Incredible Demos

  • By
    MooTools Equal Heights Plugin:  Equalizer

    Keeping equal heights between elements within the same container can be hugely important for the sake of a pretty page. Unfortunately sometimes keeping columns the same height can't be done with CSS -- you need a little help from your JavaScript friends. Well...now you're...

  • By
    Create a Quick MooTools Slideshow with Preloading Images

    I've been creating a lot of slideshow posts lately. Why, you ask? Because they help me get chicks. A quick formula for you: The following code snippet will show you how to create a simple slideshow with MooTools; the script will also...

Discussion

  1. I’m most comfortable with php and asp, but what happens when you’ve been coding straight html (index.html), and you move to a new domain? How do you not lose search engine ranking when you have to use asp or php for your 301 redirect? Isn’t search engine ranking based on the actual page (not the domain)?

    Did I shoot myself in the foot by having done all the SEP for my *.html pages, or can a simple move to default.asp with the redirect work?

    There’s one other proposition, and that’s that I’m missing something entirely here (like some code to put on the server that does the redirect for me).

    Could someone help me out here?

  2. hi all:

    I’m using the php redirect statements, but got error: “Warning: Cannot modify header information – headers already sent by (output started at /hermes/bosweb/web058/b580/sl.poolspahelpercom/public_html/contact.php:10) in /hermes/bosweb/web058/b580/sl.poolspahelpercom/public_html/contact.php on line 82”

    line 10 is where the tag starts at

    line 82 is where the header(‘HTTP/1.1 301 Moved Permanently’);
    and line 83 is where
    header(‘Location: http://poolspahelper.com/index.html‘);
    are located at

    How do I solve that?

    Thanks

  3. …correction to my previous post, missing tag &lt head &gt in line 10, hope this time goes out well

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!