How to redirect a web page, the smart way

The internet today is full of webmasters that are always updating, editing and even deleting web pages.

Lets say you are updating your website completely, changing the names of page's filenames (ex: file.html to file.php) and so on, this is great, you should stay updated! But what if you want to get rid of those old pages without having to worry about those who go to the old web page and see nothing? It doesnt end there either, other visitors do include major search engines such as MSN, Google and Yahoo! If people are finding your old pages when querying in these search engines, and they attempt to go to that page that has been deleted or moved, they will get a "404 File Not Found" Error! Now i know you dont want that, no webmaster wants that!

UPDATE: For those of you still confused on what web page redirection is, I have written a follow-up article titled Understanding Web Page Redirection, the smart way, to help answer some of the questions I most commonly get in the comments of this article.

The 301 Redirect

The best way to redirect those pages is by using something called a "301 Redirect". What this 301 redirect does, is it blatantly redirects to a different page when it is triggered, what makes the 301 redirect the best, is that not only does it accomplish your redirect, it does it safely, no having to worry about the search engines penalizing you for it! To be specific, the 301 redirect tells the browser, or in other cases, it tells the search engines "Hey this page has been moved, here is the correct URL!". Think of it as you getting mail that is not addressed to your name, possibly addressed to somebody who has lived there prior to yourself, what do you do? You tell the post man (or woman) "Hey they dont live here anymore, here is the correct address". It is the same concept guys, pretty simple if you asked me!

So lets get started. Below you will see several methods of using the 301 redirect, including the redirect in PHP, the redirect in ASP, the redirect in ASP .NET, the redirect in JSP (JAVA), the redirect in IIS, the redirect in ColdFusion, the redirect in CGI/PERL and finally the one I find most useful, the redirect using htaccess. Also showing other useful ways of using the 301 redirect with mod_rewrite!

HTML Redirection

How do you redirect using html you ask? Here is how: DONT!

Over the past 4-6 years, use of meta tag refresh redirection has been abused for uses in relation to SPAM. The result of this and other scenarios of mis-uses of it, is that when using it, that page WILL be de-indexed from every search engine.

NOTE: This also applies to javascript redirection. Search engines can easily detect javascript and meta tag redirection, so just dont do it, use the 301 redirect.

301 Redirect Using htaccess

Using htaccess to accomplish the 301 redirect is highly suggested due to it being fairly convenient to manage, rather than setting redirects on each individual page, you can simply add the redirect code to the .htaccess file.

Here is how to do it:

  1. Create a file on the root directory of your website, name it ".htaccess".
  2. Open the .htaccess file using notepad or what ever text editor that you prefer.
  3. Add this into the .htaccess file, save it and then upload it to your web server:
    CODE:
    1. Redirect 301 /old/old.html http://www.you.com/new.html

NOTE: Don't add "http://www" to the first part of the statement - place the path from the top level of your site to the page. Also ensure that you leave a single space between these elements:

redirect 301 (the instruction that the page has moved)
/old/old.html (the original folder path and file name)
http://www.you.com/new.html (new path and file name)

Also note that you are not required to redirect the page to another domain, an equally useful purpose for using the 301 redirect, is redirecting old pages to the new pages on the same domain, it all works the same way!

UPDATE: .htaccess Editor is a simple, yet useful resource for generating htaccess files.

301 Redirect Using Mod_Rewrite

Mod_Rewrite has got to be one of the most usefull modules a server can have in terms of SEO, it allows to organize the file structure of your web site in a dynamic yet simple fashion, in this example I show a useful method of 301 redirecting with mod_rewrite.

When somebody links to your website, sometimes they dont always link to you in the way that you want them to. If somebody links to www.yoursite.com and somebody else links to yoursite.com, Google will assign a separate pagerank for each of those. Yes, it is stupid but it is true, by inserting the below example into your .htaccess file, it will solve the problem by redirecting anything linking to yoursite.com to www.yoursite.com, also redirecting the pagerank, so no worries!

CODE:
  1. RewriteEngine On
  2. rewritecond %{http_host} ^yoursite.com
  3. rewriteRule ^(.*) http://www.yoursite.com/$1 [R=301,L]

301 Redirect Using IIS

  1. In internet services manager, right click on the file or folder you wish to redirect.
  2. Select the radio titled "a redirection to a URL".
  3. Enter the page that the page will be redirected to.
  4. Check "The exact url entered above" and the "A permanent redirection for this resource".
  5. Click on 'Apply'.

301 Redirect Using ColdFusion

As well as many server side scripting languages, using the 301 redirect in them is fairly simple.

Simply add this code to your ColdFusion page:

CODE:
  1. <cfheader statuscode="301" statustext="Moved permanently">
  2. <cfheader name="Location" value="http://www.new-url.com/">

301 Redirect Using PHP

Simply add this code to your page or script:

PHP:
  1. <?
  2. header( "HTTP/1.1 301 Moved Permanently" );
  3. header( "Status: 301 Moved Permanently" );
  4. header( "Location: http://www.new-url.com/" );
  5. exit(0); // This is Optional but suggested, to avoid any accidental output
  6. ?>

301 Redirect Using ASP

Simply add this code to your page or script:

ASP:
  1. <%@ Language=VBScript %>
  2. <%
  3. Response.Status="301 Moved Permanently"
  4. Response.AddHeader "Location", "http://www.new-url.com/"
  5. %>

301 Redirect Using ASP .NET

Simply add this code to your page or script:

ASP:
  1. <script runat="server">
  2. private void Page_Load(object sender, System.EventArgs e)
  3. {
  4. Response.Status = "301 Moved Permanently";
  5. Response.AddHeader("Location","http://www.new-url.com/");
  6. }
  7. </script>

301 Redirect Using JSP/JAVA

Simply add this code to your page or script:

JAVA:
  1. <%
  2. response.setStatus(301);
  3. response.setHeader( "Location", "http://www.new-url.com/" );
  4. response.setHeader( "Connection", "close" );
  5. %>

301 Redirect Using CGI/PERL

Simply add this code to your cgi/perl script:

PERL:
  1. $q = new CGI;
  2. print $q->redirect(" http://www.new-url.com/ ");

301 Redirect Using Ruby/Ruby on Rails

(Thanks to Codeninja) Simply add this code to your ruby/ruby on rails script:

RUBY:
  1. def old_action
  2. headers["Status"] = "301 Moved Permanently"
  3. redirect_to "http://www.mynewpageorsite.com/"
  4. end

Pleaee note that all of the snippets of code above are examples and I have tested each at some point. However, I am in no way responsible for any damage the code may cause, you use this code at your own risk.

409 Responses to “How to redirect a web page, the smart way”

Pages: « 4113 12 11 10 9 [8] 7 6 5 4 31 » Show All

  1. Jay Reding said,

    June 29, 2006 @ 5:14 pm

    In Ruby on Rails, if you’re rerouting someone to another part of your application, you’d need to use routes instead.

    To do that, just add a line to config/routes.rb like:

    map.connect ‘old_action’, :action => ‘new_action’

    Or you can even send them to a completely new controller.

    map.connect ‘old_action’, :controller => ‘new_controller’, :action => ‘new_action’

    That doesn’t generate a 301, but it does seamlessly redirect people within your application. If you’re redirecting someone to another page outside the application, the other way is the way to go.

  2. jim said,

    June 28, 2006 @ 4:58 am

    Question for you 301-meisters:

    I use an apache htaccess file to get ‘friendly’ urls, basically setup to a) make it all www then b) map a non-ended url to the correct file ending.

    All good but I would like to have a list of possible mistyped urls with a 301 redirect a-la methods outlined here. Eg. a signing page at http://www.x.com/pages/signin, if someone types http://www.x.com/signin it would be good if they ended up at the right page.

    I’ve tried this method (between the www-rewriter and the ending-director):
    Redirect 301 /signin http://www.x.com/pages/signin

    but it ends up a ’server loop’ as the redirected ending is the same as a the flagged ending, so I guess it tries to find (in an infinite loop):

    http://www.x.com/pages/http://www.x.com/pages/http://www.x.com/pages/…

    Any ideas?

  3. GayLjungberg said,

    June 27, 2006 @ 10:16 pm

    Thanks for a great resource!!!!

    I have totally rebuilt my site (a webshop) from static htmlpages to dynamic asppages.
    In the old structure there was “pages”, in the new structure there are “products”.
    In other words I cannot move page a to page b using your advice for 301 Redirect Using IIS. Is it any point in redirecting a folder (containing several html pages) to the index.html at least bringing in visitors through the new the front door. The alternative would be to delete the old files and let them see a 404 message instead. And what would be the effekt on SE. (The old pages are from 1996- and well indexed and ranked fairly high)

  4. Vikram said,

    June 26, 2006 @ 7:13 pm

    I’m trying to implement this in JSP. My code is:
    if (x) {
    response.setStatus(301);
    response.setHeader( “Location”, “http://www.vermontcountrystore.com/” );
    response.setHeader( “Connection”, “close” );
    }
    y statements;

    This code is enclosed inside an if block as shown. There is a bunch of other code below this if block (y statements).

    My concern is that the code after the if block is also getting executed. The redirect must happen inside the if block and control should not reach the y statements. But unfortunately this is not happening.

    The moment I comment the code for y statements, redirection works fine which ensures that the code is correct for redirection. Looks like I have to do some flushing or something soon after the last line of redirection?

    Please advice.

  5. Fred said,

    June 26, 2006 @ 1:35 pm

    Is there a way - using your first .htaccess method - to add a message to flag the change of address to the user while the redirect is under way? Would this slow things down?

    Thanks for the tips - I would have had no idea about how to do this without being penalised by the search engines.

  6. James said,

    June 25, 2006 @ 4:50 pm

    Hi Steven,
    I am having a problem with google sitemaps and my 301 redirect in my htacess file. It looks like google is mixing up my redirects and in turn creating 404 not found errors. For example, I have a 301 redirect to http://www.runningrascals.com/about%20us/index.htm, but for some reason it is picking up other words and placing them at the end of the redirect like this:
    http://www.runningrascals.com/about%20us/index.htmindex.htm
    or
    http://www.runningrascals.com/about%20us/index.htmsunnyisles.htm.

  7. beatnik said,

    June 23, 2006 @ 4:19 pm

    You forgot to load the CGI module in your perl example. Add a ‘use CGI;’ before those lines (without quotes, obviously).

  8. Don Strack said,

    June 22, 2006 @ 3:27 pm

    I converted all my pages to PHP by doing the coding, then saving them with the same name, but with .php as the extension instead of .htm. Trouble is, the search engines keep finding links to the old .htm pages.

    Is there some way of globally redirecting an .htm page to its current .php equivalent?

  9. Chris said,

    June 22, 2006 @ 6:05 am

    When using the PHP redirect, I usually add :

    exit(0);

    after the redirect so that the remainder of the page does not get parsed. This may help speed things up and halt the page from any further updates it may make.

  10. Philippe Leblond said,

    June 22, 2006 @ 3:29 am

    This 301 redirect has solved an annoyance that has lasted years for me when my first web host was sold and I had to get my own domail name and transfer all my website.

    Thank you

Pages: « 4113 12 11 10 9 [8] 7 6 5 4 31 » Show All

Leave a Reply