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? 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 don't live here anymore, here is the correct address". It is the same concept, pretty simple stuff.

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

Simply add this code to your ruby/ruby on rails script:

RUBY:
  1. head :moved_permanently, :location => "http://www.domain.com/

Zachary Pinter has a nice explanation of this.

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.

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

Pages: « 6754 53 52 51 50 [49] 48 47 46 45 441 » Show All

  1. Eric said,

    January 13, 2009 @ 2:34 pm

    How can I keep the redirect pages url: http://www.mysite.com/123.php in the browser after the destination page is loaded?

  2. SS said,

    January 11, 2009 @ 12:43 am

    Thanks alot! Your suggestion works for me! now i can direct mydomain.com to http://www.mydomain.com as I wish using htaccess code.

    Thanks again!
    SS

  3. Jamp Mark said,

    January 7, 2009 @ 3:50 am

    hi, i have a scenario.

    if siteA randomly redirects to siteB using 301 to effectively share traffic,
    will PR of siteA transfer to siteB or both will have same PR?

    thanks

  4. Chris Mahan said,

    December 30, 2008 @ 11:18 pm

    Excellent!

  5. friend said,

    December 30, 2008 @ 10:11 am

    “refresh” meta command

    Note that this method is deprecated by the official HTML standards organization in favor of the server-based redirect method described above.

    You can set up a web page to inform any browser which happens to load it that there is another web page it should go to instead, after an optional delay.

    This is accomplished using a “refresh” meta command in the header section

    .
    .

    of your HTML file, along with the title and any “keywords” or other meta commands.
    Syntax
    The syntax for the “refresh” meta command is

    where N is the approximate number of seconds that you want the current web page to be displayed before the browser automatically goes to the other web address. If N = 0, then the browser should go immediately to the other web address.
    Netiquette tip
    In case someone’s browser doesn’t handle these automatic redirects (most browsers do handle them, but some allow them to be turned off, as a way of discouraging “web spam”, which often uses this type of “refresh” redirect), you may want to provide a second route to the intended destination by way of a standard link (see the example, below).
    Example

    A web page that points a browser to a different page after 2 seconds

    If your browser doesn’t automatically go there within a few seconds,
    you may want to go to
    the destination
    manually.

  6. Billigflüge said,

    December 30, 2008 @ 4:33 am

    Thank u for taking the time to write this article which is very helpful to me
    ” you can manually choose the domain page by going to googles webmaster page and chose the settings tab”
    I have tried it , it works
    keep up the good work
    I will be aregular reader of ur blog

  7. Dave said,

    December 28, 2008 @ 7:55 pm

    Hi Brandon

    Hey guess what i found out : Did you know that you can manually choose the domain page by going to googles webmaster page and chose the settings tab.

  8. PennySeeds.com said,

    December 24, 2008 @ 10:06 am

    Thanks a lot for this information! I was having a problem with my indexing, because of an ‘http’ and ‘http://www’ version of my site. Now it’s set to 301 re-direct to one of those, and I think this should fix my problem! : )

    Thank you!

  9. JEB said,

    December 23, 2008 @ 2:31 pm

    Hello Steve,

    Great info! Very helpful! I hope you don’t mind me asking if you’d give me some further explaination of this subject, and my apologies if my I get a little long winded. I just want to be certain I’m explaining my issue thoroughly so you understand what I’d like to do and let me know if I’m on the right track… The short version is that I don’t quite fully understand the relationship of the (single) htaccess file, and how it can be use for multiple webpages (assuming you can - as your article refers to directing old pages to new pages).

    I found you information, well, searching for a way to redirect ‘A’ web page. Originally only to direct my index.htm page to my home.htm page, but after reading your articles, and if I understand the importance correctly, I’d like set it up for other old webpages to their new webpages…

    Let me state right off the bat that I’m not a professional site designer. I have my own personal site that I created and rebuilt several times over the last ten years (and I’ve done a little on the fly coding when I needed it - learning along the way). Now I’ve taken on creating a new site for a friends small business. So after reading your articles, although I understand the concept behind and the differences between the two types of redirects, where the server side redirect is concerned I don’t understanding how I can use the htaccess file for more than one page (unless I’m mistaken and you can’t).

    Here’s my issue… As I stated, I’m creating a new website for a friends small business. I’m using the same domain, just updating and adding pages. He wanted to see the new pages on the fly, so I put them into a sub-directory to keep the separate from and to keep the old pages intact until I’m finished. I planned to keep them in the sub-directory, so now I’m looking for a way to redirect the default (index.htm) page in the main directory to the main (home.htm) page in the new sub-directory so I didn’t have two of the same pages with different names - which I didn’t think was the right way to set it up. On the old site it wasn’t an issue, because he used the index.htm page as an individual (portal) page, but he doesn’t want the portal page on the new site - he wants the customer getting directly to his home page.

    So initially, I want my index.htm page to “redirect” to the home.htm page in the sub-directory. But now after reading your article, in order to keep his ranking in the search engines, I think I should setup redirects for all the ‘old-name’ pages to the ‘new-name’ pages.

    OK, finally getting to my question… So how do I setup redirects for all these pages with a single htaccess file? Do I include an individual line in the htaccess file for each page I want redirected? Or can the htaccess file only be used for ONE page (IE the main/index page)?

    This all may be moot for me, ’cause I don’t know if the site hosting server allows for this or not (I’m assuming the htaccess file is something the host needs to be setup for, and his site is setup on one of the free hosting sites - so they may not allow it), but I’d like to fully understand the capabilities of the htaccess file to use later even if I can’t on this project.

    Your comments would be appreciated. Keep up the great work!

    Happy Holidays…JEB

  10. mike said,

    December 21, 2008 @ 4:06 pm

    If i create a .htaccess file just with one line “redirect…”, will this pose any security threats?

    I am using godaddy shared hosting linux. Just opened it up.

    Thanks,
    Mike

Pages: « 6754 53 52 51 50 [49] 48 47 46 45 441 » Show All

Leave a Reply