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: « 4115 14 13 12 11 [10] 9 8 7 6 51 » Show All

  1. Kristy said,

    July 20, 2006 @ 4:22 pm

    I’m hosting on a windows server and need to create permanat re-redirects for a dynamic page. The original page dynamiclanding.aspx was used by multiple different categories so the url would look something like dynamiclanding.aspx?CategoryID=10 or CategoryID=11 and so forth.

    I have created custom dynamiclanding pages in order to be able to optimize for the search engines. So now, I have pages like this dynamiclanding-1keyphrase-2ndkeyphrase.aspx. I need to re-direct
    http://url.com/dynamiclanding.aspx?CategoryID=10 TO http://url.com/dynamiclanding-1keyphrase-2ndkeyphrase.aspx?CategoryID=10 and http://url.com/dynamiclanding.aspx?CategoryID=11 TO http://url.com/dynamiclanding-3rdkeyphrase-4thkeyphrase.aspx?CategoryID=11

    I can’t simply redirect the dynamiiclanding.aspx to any one specific page since the variable needs to be there. The base page, dynamiclanding.aspx DOES still exist, so I’m not worried about users getting an error while the SE’s update their index to the new pages; however I want my pagerank to be tranferred. This site has very good rankings and I do want to be dropped due to having all of my pageranks drop off.

    Any help would be appreciated.

  2. anatolijd said,

    July 20, 2006 @ 2:35 pm

    i found the reason, please ignore this and my previous post, as the issue i found has nothing to do with IIS…

  3. anatolijd said,

    July 20, 2006 @ 12:56 pm

    301 Redirect using IIS 6.0 :( :)
    Hm…
    I`ve tried to do so,
    set a redirection to URL
    checked “exact ULR enetered” and “permanent redirection for this resource”,
    but IIS constantly returns “302 Redirect” instead of 301: :|

    $ telnet http://www.preved-medved.com 80
    Trying X.X.X.X…
    Connected to http://www.preved-medved.com (X.X.X.X).
    Escape character is ‘^]’.
    GET /redirect_test.html HTTP/1.1
    Host: http://www.preved-medved.com

    HTTP/1.1 302 Redirect
    Content-Length: 149
    Content-Type: text/html
    Location: http://www.google.com
    Server: Microsoft-IIS/6.0
    X-Powered-By: ASP.NET
    Date: Thu, 20 Jul 2006 08:25:54 GMT

    Has anybody ever seen this issue also? :)

  4. Randy said,

    July 19, 2006 @ 3:33 pm

    Hi Greg A.,

    I would like to implement the .NET solution you mentioned in yor post. Would it be possible for you to send me an example of this implementation? I would really appreciate it.

    Thanks!

  5. A Samuel said,

    July 19, 2006 @ 8:18 am

    Hi,

    I am not a programmer so forgive my lack of knowledge.

    My web developer has developed a site in asp on IIS6 and I have problems with the 404 page. The rewrite rules have been set up to redirect to sitemap on any files that are not asp. However on a dead page that has the extension .asp the rewrite rules behave differently and instead of a 404 it returns a 302 which google doesnt like.

    Can this be fixed easierly as my web developer says its not possible??

    Any help would be greatly appreciated.

    Thanks

    Adam

  6. Attila said,

    July 19, 2006 @ 7:42 am

    Hi,

    Here is the answer to my question posted above ( http://www.stevenhargrove.com/redirect-web-pages/#comment-214” )
    One can set up a mod_rewrite condition to test for the availability of a particular uri via a subrequest and based on the outcome perform a url rewrite and hand the request over to mod_proxy.
    The only drawback is that it hurts the performance (if the subrequest turns to be valid then the server will respond to the client by completing the original request cycle which results in doing the same thing twice).
    This overhead is not mod_rewrite specific: there is no really good solution for this problem.

    –Attila

  7. Pierre Legrand said,

    July 15, 2006 @ 6:27 am

    Have gotten my new site completely set up and spent the better part of the day writing my redirect 301 .htaccess file to redirect my 100 highest rated pages.

    First question is it possible to simply redirect all requests to my old url to my new url using 301?

    Second question which probably is a lot more important than the first….how is this done using Front Page extensions? My current host Value Web says that using .htaccess to redirect will break the old site. This is immensely frustrating as I tried and it did. It is back up now after reinstalling the front page extensions

    Any suggestions would be highly welcome.

    Thanks in advance

  8. Quick Question said,

    July 12, 2006 @ 6:19 pm

    Ok, well I worked out what my problem was (was in fact just a error on my part). I changed error_sitedisabled.php so it didn’t read from the same header file (that contained the redirect script) from the rest of the site.

    Problem: Was redirecting when it already had redirected to the correct page.

    Solution: Isolated error_sitedisabled.php with it’s only configuration, seperate from the rest of the site.

    Thanks again for the great article.

  9. Quick Question said,

    July 12, 2006 @ 5:51 pm

    This is an excellent article! I did have a question on something I have been trying to work out all day though.

    I wrote a config file to control if the website was “Disabled” or “Enabled”. But when I view the page (my guess if from an error on my part…) it keeps trying to refresh the page using that address. (To clarify, I am using PHP) Here is some clippings from my config file:

    [code]
    // This will disable the entire site (even from admins), Options: 1=Disabled, 0=Enabled
    $sitewide_disable = “1″

    // Code for disabling site (runs before any html output)
    if ($sitewide_disable == “1″) {
    header(”Location: http://www.example.com/error_sitedisabled.php“);
    exit(0);
    }
    [/code]

  10. Attila said,

    July 12, 2006 @ 3:18 pm

    I want to redirect requests to another web server when they result in 404 Not Found on my server. How can I do that without going back to the client and without mod_perl?

    Many thanks, –Attila

Pages: « 4115 14 13 12 11 [10] 9 8 7 6 51 » Show All

Leave a Reply