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.

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

Pages: « 4420 19 18 17 16 [15] 14 13 12 11 101 » Show All

  1. John said,

    December 2, 2006 @ 12:41 am

    Hi Steven,

    I hope you could help me. I appreciate a lot.
    When I click on serch engin results, they will be redirected to other sites for the first two click. Also sometimes even the URL address showing in address bar is correct but the content is something else.

    Thank you very much

    John

  2. Drew said,

    November 25, 2006 @ 2:51 pm

    Steven,

    Thanks so much for a great article. My site dropped off Google shortly after I converted my index.html to .php and put in an HTML redirect. I’m not a programmer, but I was able to make the 301 redirect work the first time. I found your article on digg, looks like a bunch of other folks did too.

    Thanks.

  3. vendude said,

    November 21, 2006 @ 10:15 am

    Please help us Obi Wan. You’re our only hope.

    We mapped a http://www.domain.com to our Typepad blog at domain.typepad.com a few months ago. With our ignorance of the way of Google, we thought it was a good idea to get our brand and have two ways to get to the site. Now our links are split, so there is no going back…and all of a sudden, though our traffic has never been higher and we’ve never had so many good links from others, our google ranks and inclusions are dropping.

    TypePad doesn’t allow htaccess
    I am not the most technical, but i don’t think we can use your other 301 techniques either.
    Got any more? Anything we can signal a 301 with in the headers or something?

    Anyone out there have a way to do the redirect at Typepad and keep the business you’ve built? this is a MAJOR failing of Google

    Is there a way to send message to Google that we would 301 redirect if we could. (one look at our site and it’s clearly not a splog)

    Would you do a meta redirect on the page rather than do nothing?

    Any insight ?

    This is urgent as we watch our Google rank and inclusion drop over days…yours seems the most complete resource to be had but i can find nothing on Typepad and domain mapping.

    Thanks for your help and for any help your readers might be generous enough to share.

    I’m going to post on Matt Cutt’s as well fyi as he had a post on this.

    thanks.

  4. Konstantinos said,

    November 20, 2006 @ 5:22 pm

    Steven, thank you for an excellent HOWTO. I would like to ask a question:

    Similar to the PHP/ASP/etc. methods, is there a code I can write in an XML file so as to have it redirect to somewhere else? I’m on a Windows server with limited access and want to redirect the site’s feed to the new feed.

    Thanks in advance for your help.

  5. Sabu said,

    November 20, 2006 @ 1:29 pm

    The 301 redirect method that you have described seems like not useful with geocities. I tried .htaccess but the file can not be uploaded. Then I used ASP .NET and JSP/JAVA scripts, unfortuneately there is no redirection occured. Please comment…

    Thanks
    Sabu

  6. Luc Dubois said,

    November 14, 2006 @ 8:30 am

    This was one of the most informative pages I’ve read on the subject this week (and I’ve been reading hundreds of pages…), not only the original posters article, but also the wealth of ideas and information in the comments! Apparently there is a lot of interest in the topic, the comments run from April to now! Congrats Steven!

  7. Steven Hargrove said,

    November 10, 2006 @ 6:57 pm

    Kenny B,

    Yes you surely can, with mod_rewrite, you can see it in the “mod_rewrite” section of this article.

    Example:

    RewriteEngine On
    rewriteRule ^file_number_(.*).html http://www.yoursite.com/newFolder/new_file_numer1.html [R=301,L]

    In this case, the (.*) registered the wildcard, and the “$1″ executes the value of it

  8. Kenny B said,

    November 10, 2006 @ 1:58 pm

    Steve,
    One last question. Can we use wlidcards to reference all of the 300 product pages in the old site?

    Example:
    Redirect 301 /old/*.html http://www.you.com/default.html

    That would save tons of time….

  9. Steven Hargrove said,

    November 10, 2006 @ 10:54 am

    KennyB:

    Does the server of the site in question allow htaccess? If so, it would probably be the best way to go about it

    in your.htaccess file, it would be as easy as something like this:

    Redirect 301 /old/old.html http://www.you.com/new.html

    Basically you would do this for every page, for example if you had 3 pages that needed to be redirected:

    Redirect 301 /old/old.html http://www.you.com/new.html
    Redirect 301 /old/old.html http://www.you.com/new.html
    Redirect 301 /old/old.html http://www.you.com/new.html

    Or, if the pages in question were dynamic in some way (php, asp, query string-related URL?), you could set up that same file name (ex: if the page was in php, create the php page and just add the PHP 301 redirect code)

    Hope that helps!

    LHJackson :

    I can’t speak for the search engines, but I will say that using 301 redirects as your prime method of page redirection has been pretty much adopted as a standard. As far as I know, it is by far the safest and most reliable way of redirecting web pages

    My advice: Don’t be afraid of the 301 redirect unless your intentions are Black-hat SEO.

  10. Kenny B said,

    November 9, 2006 @ 5:08 pm

    Steve,
    I have taken quite a bit of time reading through your forum here and I’m very impressed, but I must be missing something. All that I have read seems to be based on the same valuable assumption: that the developer has access to the previous web. I can’t add redirect code to a page that is no longer accessible.

    Client’s dilemma -
    Our client hated his old site so much that he called up his “tech hosting guy” and told him to cancel everything without considering the consequences. He’s loves the new site (”skipsboots.com”), but now that the new site is live, he’s realizing that all of the google and yahoo search engine listings no longer work (”404 Page cannot be displayed”) because nearly all of them link to pages from his old site that no longer exists.

    Our Dilemma -
    He has challenged us to figure out a way to salvage his rankings and listings by having all the old links redirect to his new site. The problem is we no longer have access to the old site. The pages are gone and the web has been deleted by his old developer/host that he fired. The only common bond between the two sites is the root domain(”skipsboots.com”). Once you get into the web structure, everything changes beyond that. We are not tryng to map products and pages to the new site, we would be perfectly content to redirect them all to the home page and let the customer browse from there since his product line and content is much different now.

    Any ideas?

    p.s. We trap 404 codes for our URL rewrite logic that directs and creates the custom pages for each category and product in the shopping cart.

Pages: « 4420 19 18 17 16 [15] 14 13 12 11 101 » Show All

Leave a Reply