Permanently Redirecting URLs using 301 Headers in Static and Dynamic Sites

Permanently redirecting traffic using a 301 header is the proper and fail-safe way to ensure that your web-visitors continue to find content that has permanently moved to a new locations. Search engine ranking is crucial for any business. Web infrastructure changes and changes in best-practices can lead to a change in the URI of content items. Being that Search Engine indexes take time to update (10 days to 3 months), it is important to continue to maintain the old URLs until all the external referrers are updated.

Why there may be a need to use new paths

It is probable you have gained some knowledge and technologies have changed since you last overhauled your website and you may have a new naming system for your paths that's uniform and that hopefully embeds keywords in your document paths to maximize your ranking advantage. This situation makes it necessary for a web producer to create new URL paths for existing documents while keeping the old URLs active until everyone is updated.

How to implement

You cannot reliably depend on client technology to handle any tasks beyond the display of simple standards-compliant markup. Therefor you can employ a number of methods on the server to implement 301 Header redirects as follows:

Server-side Implementation

Windows IIS with Active Server Pages (ASP)
<%@ Language=VBScript %>
<%
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", "http://newpagelocation.com/path"
Response.End
%>

PHP
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: /my-new-path");
exit();
?>

ColdFusion
<CFHEADER statuscode="301" statustext="Moved Permanently">
<CFHEADER name="Location" value="http://newpagelocation.com/path">
mod_rewrite (Apache Web Server)
rewriteEngine on
rewriteRule ^contact\.php$ http://newpagelocation.com/path [R=permanent,L]

Addendum for Web Content Management System

Placing this code in the template and testing for the requested URL to see if the page has been redirect can add a heavy overhead that could slow or even cause errors in your system. It is better to create placeholder pages with the URL of the removed pages and place the 301 redirect code for each case.

NB: If your CMS can schedule publishing and archival of pages, you can determine when to retire the redirect for a given page (estimate 6 months for all Search Engines to update their indexes)

For example, to implement this in Drupal:

  • Create regular node of any node type
  • Select the the PHP content type
  • Place the following code in it and modify the destination URL to the new path alias (for engines to index the new name as opposed to the node ID)
  • Create a path alias for this document bearing the URl of the OLD path Save and publish the document

 _________________________

How it works

Since you create a document with the same path as the old location, when the old URL is called, the redirecting node will 301 redirect it to the new location thereby enabling you to maintain your old URLs while promoting your new paths and indicating to Search Engines that your page has moved.

NB: Sending a 301 header redirect is the best and proper way to redirect permanently moved pages.

Client-side Redirection
As explained at the beginning of this document, the only reliable and proper way to redirect traffic from a non-existent page and properly signal the new location is to use the 301 Header redirect. In the absence of access to server technologies, the next best thing is to use client-side scripting and META REFRESH headers to redirect traffic. These methods do not indicate a permanent change or URI and bookmark and index driven agents such as search engines and link referrers will not know that your content has moved unless a human-being manually modified the path.

Javascript
<script type="text/javascript">
window.location.href='http://newpagelocation.com/path';
</script>

META REFRESH

<html>
<head>
<meta http-equiv="refresh" content="0;url=http://newpagelocation.com/path">
</head>
<body>
This page has moved to <a xhref="http://newpagelocation.com/path"> The content has moved to http://newpagelocation.com/path</a>
</body>
</html>

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

missing info

Hi - nice blog.
Looking at migrating a bunch of pages in drupal, and your 301 redirect instructions were crystal clear.

Seemed to be missing the code snippet after this paragraph: "Place the following code in it and modify the destination URL to the new path alias (for engines to index the new name as opposed to the node ID)"

Although pretty intuitive - make a new drupal page with the identical URL alias, use the PHP code snippet above with l(...) - or maybe url(...)

NIce post - should put it into the drupal handbook somewhere.

Why not use .htaccess to handle the 301's?

Much easier - especially than generating postentially hundreds of dummy pages - to just add to .htacces file - at least works on Apache.

#custom redirects
RewriteRule ^oldurl$ http://fullpath/to/newpage.html [R=301,L]
#end custom redirects

Path Redirect Module

There is a new drupal module that enabled you to create 301 redirects from within the Drupal application. This is an alternative tot he process detailed above.

http://drupal.org/project/path_redirect

.htaccess works too; why I suggested this method

.htaccess works to implement 301 redirects perfectly. The justification for my PHP based solution was to be able to implement this per-page and to take advantage of some Drupal features... in particular, to be able to schedule when a redirect should go live and be able to determine exactly when to retire a 301 redirect (after search engines ave updated their indexes/indices)

Global Redirect Module in Development

While on the subject of useful 301 redirects, there is a module in development that enables a Drupal CMS administrator to specify a 301 redirect destination for a given page.
Global Redirect
Valid XHTML 1.0 Strict
This site is accepts Oped ID authentication for login
This Website is Built Using Semantic Markup and Cascading Style Sheets (CSS)
Some usage rights are reserved, please contact us for approval before using it