The 301 Permanent Redirect

There are several cases where a web page or even whole website is moved to a new location.

  • Page Name Change
  • Website Upgrade
  • Domain Change or Consolidation

A common mistake is to just make the move without considering search engines and visitors experiences. This can lead to visitors experiencing broken links and search engines dropping their index for your pages.

To resolve this you should tell your visitors and the search engines where the page has moved to. And the best way to do this is via a mechanism called 301 Permanent Redirects. Browsers support this and seamlessly move the visitor to the new page. Search Engines respect it and transfer their index data to the new page.

There are several other redirect mechanisms available, however the 301 Redirect is the only one that Search Engines (in particular Google) state they support for updating their index.

How you perform 301 Redirects varies with the system you use and the nature of your problem. The rest of this article explains several of the different ways to implement 301 Permanent Redirecting.

You can use my Redirect Header Checker to confirm if you have setup redirects correctly and that they are really of the 301 type.

PHP in File 301 Redirect

If you're using PHP and your old page is a PHP page, you can change it's content to perform a redirect in the following way.

<?
Header("HTTP/1.1 301 Moved Permanently"); 
Header("Location: http://www.website.com/new-page/"); 
?>

Apache .htaccess 301 Redirect

If you are running your website on an Apache server then you can use the .htaccess file to setup all your redirects. This is more manageable as you don't need to keep old files and it also works for any URL (not restricted to PHP files).

The method is based on editing a file called .htaccess which needs to be placed in the root of your website. This file is used for several purposes so make sure you use the existing one if it is already there.

Here is a example of a basic .htaccess file that does a few 301 Redirects

# Should be before any rewrite commands, only required once
RewriteEngine On    

# 301 Redirects
# Place these before any rewrite code such as in Joomla or WordPress
RewriteRule ^old-page\.htm$  http://www.website.com/new-page [R=301,NC,L]
RewriteRule ^old-page2\.htm$  http://www.website.com/new-page2 [R=301,NC,L]
    

I've also written a tool to help you Generate an .htaccess file from a list of pages.

ASP in File 301 Redirect

If you're using ASP and your old page is an ASP page, you can change it's content to perform a redirect in the following way.

<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www.website.com/new-page/"
%>

ASP.Net in File 301 Redirect

If you're using ASP.Net 3.5 or less and your old page is an ASP.Net page, you can add the following to the Page_Load method.

try
{
    this.Response.Redirect("http://www.website.com/new-page/", true);                        
}
catch (Exception)
{
    // this is to make the redirect a 301 type
    context.Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
    throw;
}

In ASP.Net 4.0 you can just do this


    this.Response.RedirectPermanent("http://www.website.com/new-page/");                        

If you chose to instantly end the response (true) then the method will trigger a ThreadAbortException, so make sure your exception handling does not do strange things because if this.

IIS 7 Routing Module and web.config

In IIS 7 and .Net 3.5 the Routing Module was introduced (You may need to install and enable it). It lets application developers control URL rewriting and redirecting via code or even in the web.config file. Here's an example:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="301 Redirect 1" stopProcessing="true">
          <match url="^old-page\.htm$" />
          <action type="Redirect" url="new-page" redirectType="Permanent" />
        </rule>
        <rule name="301 Redirect 2" stopProcessing="true">
          <match url="^old-page2\.htm$" />
          <action type="Redirect" url="new-page2" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>                       

The same idea can be used to setup a redirection system without the need for pages to be present. This website uses Route Handlers and Http Handlers to process all requests including the handing of 301 redirects.

Zeus Server Scripts

If your php website is powered by a Zeus server then you can't use the .htaccess file. On Zeus you need to create a rewrite.script. Here's an example:

insensitive match URL into $ with ^/old-page\.htm$
if matched
match IN:Host into % with ^(.*)$
set OUT:Location = http://%1/new-page
set OUT:Content-Type = text/html
set RESPONSE = 301
set BODY = Moved
goto END
endif

# 301 Redirect 2
insensitive match URL into $ with ^/old-page2\.htm$
if matched
match IN:Host into % with ^(.*)$
set OUT:Location = http://%1/new-page2
set OUT:Content-Type = text/html
set RESPONSE = 301
set BODY = Moved
goto END
endif
                       

I've written a Zeus Server 301 Redirect Script Generator Tool to help you there as well.