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
    Being a Dev Dad

    I get asked loads of questions every day but I'm always surprised that they're rarely questions about code or even tech -- many of the questions I get are more about non-dev stuff like what my office is like, what software I use, and oftentimes...

  • 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
    jQuery Link Nudge Plugin

    A while back I debuted a tasteful mouseover/mouseout technique called link nudging. It started with a MooTools version and shortly thereafter a jQuery version. Just recently Drew Douglass premiered a jQuery plugin that aimed at producing the same type of effect.

  • By
    Degradable SELECT onChange

    Whenever I go to Google Analytics I notice a slight flicker in the dropdown list area. I see a button appear for the shortest amount of time and the poof! Gone. What that tells me is that Google is making their site function...

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!