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: « 4116 15 14 13 12 [11] 10 9 8 7 61 » Show All

  1. michael said,

    August 10, 2006 @ 8:10 pm

    We recently converted a site from .asp to .NET. The site address hasn’t changed officially, but the indexed pages on Google are incorrect (IE: http://www.mysite.com/index.asp) doesn’t exit any longer, but the base URL is still there). Most people won’t necessarily click on the 404 error link to the base URL.
    In IIS, all we’ve done is redirected to the new site folder so I can’t do it in IIS. I’m curious if there’s a simpler method other than recreating each former page as a redirect (in asp or html) to it’s new, improved page? I only need to do this until our site is re-indexed on the search engines.

  2. NinjaDoll said,

    August 9, 2006 @ 7:27 pm

    Great info, thank you very much! I would like to ask if it’s possible to use .htaccess to redirect the main site’s index.php to an index.php in its own subdirectory (www.siteA.com/index.php to http://www.siteA.com/newinfo/index.php) without breaking other redirects to other subdirectories. What I’m trying to create is:

    http://www.siteA.com/index.php to http://www.siteA.com/newinfo/index.php
    http://www.siteB.com/index.php to http://www.siteA.com/siteB/index.php
    http://www.siteC.com/index.php to http://www.siteA.com/siteC/index.php

    It seems that once you redirect the main index.php file to a subdirectory, it breaks any other redirects to subdirectories on the same site. So nothing else works on the site except the very first redirect. Is there a way to work around this without having to rebuild the site?

  3. Chris said,

    August 7, 2006 @ 4:00 pm

    I need to do a 301 redirect for an old site I have. We made a new site to replace the old site, but we have kept the old site because google likes it. I want to use a 301 redirect to tell google to use the new page now. Our server uses linux. I don’t see linux on your list of 301 redirect options. Maybe Linux has nothing to do with it. I tried the htaccess file but was unable to create the file. Is there a specific way to do this with a linux server.

  4. Radu said,

    August 6, 2006 @ 10:15 am

    Hello Steve,
    I have a question which i believe is related to this. When i access my homepage http://www.intelligentit.ro i see a page rank of 1. This is actually the index.php. If i access directly http://www.intelligentit.ro/index.php i see a page rank of 0. How does the redirection from the first to the 2nd link occur? Is this a 301 redirection? If not, how can i make it as one so i don’t lose any page rank between the root directory and the index.php.
    Thanks, Radu

  5. Jonathan said,

    August 3, 2006 @ 1:47 pm

    Thank you so much for these scripts and this service. I tried using the asp script so the entire page looks like the page below. However, it does not work. When I go to the page that should redirect it just shows the code rather than actually going to the new page.

    Can you help? Thank you,

  6. George Kwenya said,

    August 1, 2006 @ 1:31 pm

    Hello Steve,
    I am to redirect any user who tries to access a password protected page (e-learning) to the login page or registration page and after that redirect the user back to the page he/she is trying to acess. Please how can I fix this problem. Thank you.

  7. Christopher said,

    August 1, 2006 @ 4:40 am

    In the .htaccess file, how do I redirect all pages within a certain directory to go to our index.htm page? Would the below work?

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

  8. Jay Heuer said,

    July 31, 2006 @ 8:29 am

    Dude, you are a saint on some of these questions! Geeeesh

  9. Michael Conquest said,

    July 26, 2006 @ 8:53 pm

    Great article Steven! Very helpful…I had been using a meta refresh redirect for my main domain name redirecting to a split test. Looks like I’ll be changing this using either the htaccess or php methods. I’m just now setting up a site-map and noticed only 1 page was indexed with the sitemap so that tells me that Google doesn’t like my meta-refresh as since it’s redirecting it can’t crawl the rest of my site with the sitemap?

    Any comments you could provide just send me an email…

    Thanks,
    Michael

  10. Free Articles said,

    July 24, 2006 @ 4:02 am

    How can we redirect a .html page to any other url.

Pages: « 4116 15 14 13 12 [11] 10 9 8 7 61 » Show All

Leave a Reply