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
    JavaScript Promise API

    While synchronous code is easier to follow and debug, async is generally better for performance and flexibility. Why "hold up the show" when you can trigger numerous requests at once and then handle them when each is ready?  Promises are becoming a big part of the JavaScript world...

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

Incredible Demos

  • By
    Table Cell and Position Absolute

    If you follow me on Twitter, you saw me rage about trying to make position: absolute work within a TD element or display: table-cell element.  Chrome?  Check.  Internet Explorer?  Check.  Firefox?  Ugh, FML.  I tinkered in the console...and cussed.  I did some researched...and I...

  • 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!