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 8-10 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.

Now, this doesn't necessarily apply to *everything*. If it is used properly, then you have nothing to worry about. For example, often on many forum engines - you will see when you perform different tasks such as making a thread, replying to one or even things like logging in - you will notice it takes you to a "please wait a moment" screen. On those types of pages, they are using either javascript or meta tag redirection, and its perfectly legit.

Canonical Links

My first impression of canonical links is "Great, but where the hell were you 10 years ago?". That aside, from what I see they really are for one purpose: dynamic content.

To put that into perspective, I have a wordpress plugin on this blog that will separate my comments onto multiple pages, so I don't have to show 500+ comments on one single page. So it paginates the comments for me.

The slight drawback to this, is that since it is paginating my comments onto several pages - this exact article data exists on those pages as well. So those page url's end up looking like this: http://www.stevenhargrove.com/redirect-web-pages/comment-page-50/. When really, it is just the same page with different comments.

Using canonical links, I could specify in the of the html document my canonical link. Which would look like this:

HTML:
  1. <link rel="canonical" href="http://www.stevenhargrove.com/redirect-web-pages/" />

Another interesting bit to consider about canonical links:

Can this link tag be used to suggest a canonical URL on a completely different domain?
No. To migrate to a completely different domain, permanent (301) redirects are more appropriate. Google currently will take canonicalization suggestions into account across subdomains (or within a domain), but not across domains. So site owners can suggest www.example.com vs. example.com vs. help.example.com, but not example.com vs. example-widgets.com.

Matt Cutts has a great article that shows how to use these more in depth. He also has a slide show that covers it a bit.

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.

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

Pages: « 5817 16 15 14 13 [12] 11 10 9 8 71 » Show All

  1. Benno Moiseiwitsch said,

    September 6, 2006 @ 5:36 am

    Hi Steven, I have a number of old links I need to redirect. I want to redirect the old pages to one of a variety of new pages on my site. Is there any chance you could be so kind as to let me know what the htaccess file would look like to manage this process of multiple redirects? Thanks in advance, Ben.

  2. Jack said,

    September 2, 2006 @ 12:43 pm

    I’m afraid that what I want might not be possible, unless with a lot of work, but here’s my problem:
    I rebuild a site for a customer. Formerly it was made with dreamweaver and the content of the site was placed in a folder called /html. In this folder there were many subfolders containing a lot of different html files.
    I rebuild it using wordpress and now the content of the site is in /site with a normal wordpress structure of the folders.

    What I actually need is some kind of redirect that makes ANY call to ANY file or (sub)folder within/underneath /html to go to /site/index.php.
    Can this be done?

  3. Michael said,

    September 1, 2006 @ 4:18 pm

    I took the htacess option and everything went well.

    Thanks!

    Now, another important question: do you kow how to stop hair loss? (Just kidding)

  4. Jeff said,

    September 1, 2006 @ 3:36 pm

    I have a website that has excellent positioning in Google but I have been afraid to update the website fearing I will lose positioning within the search engines, will I be safe using the 301 redirect as you have described. Thank you for your help.

  5. Pieter said,

    August 29, 2006 @ 12:50 pm

    I have a question…

    How do you redirect and maintain a path? Say I want to do a masked redirect of a page (www.joysof.com/2006/08/atom-feed-live.html) to my wacky address but maintain the /2006/08/ path.

    It will also have to do this for every future file and directory too since the site is constantly updated

  6. Steven Hargrove said,

    August 28, 2006 @ 1:34 pm

    Hooollly cow…. I really wish I had time to asnwer all of your questions…. perhaps in another post… Sorry to those who had questions that I was not able to answer, there is not enough time in each day!

  7. Eva said,

    August 24, 2006 @ 8:34 pm

    Steve,
    Thanks so much for the awesome info. I outsourced the development of my website to a team in Romania who is simply not available to fix bugs or answer questions in a timely way, so I am having to do it myself. One of the things that they did (my site originally dealt with photography and videos) was to dynamically assign numbers to products, such that when you click on the product for detail, you get an url that looks like this:

    touchinfinity.com/product_large.php?pid=31

    First of all, some submissions won’t accept the URL, because of the ?pid=31 part. Secondly, this product is a unique Hawaiian Sunrise Meditation Video that I would like to submit as a ‘meditation video’. So I want the page name to reflect the product. When I tried to use the 301htaccess method and I typed in the new name (e.g. touchinfinity.com/meditation_dvd.htm/, it did not redirect back to the correct .php?pid=31. It just showed the word ‘OLD’, which I had typed in the htm page. I’m wondering, did I put the file in the wrong place? (I’m being hosted by a company so I’m not sure if I’ve put the htaccess file in the root or not - it’s in the same directory as my index.php file. Any help is greatly appreciated!

    Thanks!!

  8. Amit Chouhan said,

    August 23, 2006 @ 1:36 pm

    Hi Steve,
    I am using the PHP language for this,
    but i having one problem i want to keep the URL of my old site in the address bar and want to redirect it on the new page,so that google rank my url.
    is it possoble to redirect the page to new URL and show the old URL in the address bar.
    Please help me to solve my problem.

  9. Dave said,

    August 17, 2006 @ 10:57 pm

    Nick: on Windows servers, go to manage your IIS site (r-click my computer > manage > Services and Applications > Internet Information Service > Web Sites > [your site]). If you want to redirect the entire site, right click its name and select Properties. Then go to teh Home Directory tab and follow the steps in Steven’s info above. If you want each file to go to its maching file in the new location then DON’T check the “The exact URL” box and the client/search engine will find the same file relative to the new URL you specified. Also, don’t put a “/” at the end of that URL.

    MY question: I followed these instructions for IIS and it worked great. But my subdirectories, like /images and /css are still accessible at the old location. Do I have to give each subdirectory the same treatment?

  10. Nick said,

    August 16, 2006 @ 6:05 am

    My new hosting service uses Windows servers. Most of the solutions discussed above are for Apache servers. Several people have asked how to do a 301 redirect on a Windows server, but the answers to those questions are few or missing entirely.

    I moved my entire site from my old URL and hosting service to a new URL and hosting service. I had my old domain registrar change settings to point to the new hosting service DNS. On the new hosting service I set my old domain name as an alias for my new domain name. Both domains now resolve to the same IP address, so I have no way to set up a redirect on the old domain that won’t be seen on the new domain, since they are the same.

    The result is that Google is still indexing my site under the old domain as well as under the new domain, which has been active for 6 months now. My site is in 1st place on several vital Google searches, but with the old domain. How can I fix it so Google only indexes under the new domain without simply dumping the old domain, and with it my high page rank and 1st place search position? My site is entirely static HTML coded. Thanks for any help.

Pages: « 5817 16 15 14 13 [12] 11 10 9 8 71 » Show All

Leave a Reply