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
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

Incredible Demos

  • By
    Introducing MooTools NextPrev

    One thing I love doing is duplicating OS functionalities. One of the things your OS allows you to do easily is move from one item to another. Most of the time you're simply trying to get to the next or the previous item.

  • By
    CSS Kwicks

    One of the effects that made me excited about client side and JavaScript was the Kwicks effect.  Take a list of items and react to them accordingly when hovered.  Simple, sweet.  The effect was originally created with JavaScript but come five years later, our...

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!