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
    9 Mind-Blowing WebGL Demos

    As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us.  Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos.  Another technology available...

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

Incredible Demos

  • By
    Fade Images with MooTools LazyLoad

    I recently received an email from a MooTools developer asking a great question about my LazyLoad class: "I'm using your LazyLoad MooTools plugin (which is great, by the way). I have been trying to figure out how to modify it so that once an image scrolls into...

  • By
    Highlighter: A MooTools Search &#038; Highlight Plugin

    Searching within the page is a major browser functionality, but what if we could code a search box in JavaScript that would do the same thing? I set out to do that using MooTools and ended up with a pretty decent solution. The MooTools JavaScript Class The...

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!