How to setup a 301 Permanent Redirect

In our previous post SEO Considerations When Redesigning a Website we touched on how to mitigate the ill effects of changing your website domain or file names by using 301 Permanent Redirects. Here we will give you instruction on how to set up this redirect on both Apache and Microsoft IIS web servers.

The “301 Permanent Redirect” is the most efficient and search engine friendly method for redirecting websites. Situations include:

  • To redirect an old domain name to a new domain name – normally in the case of re-branding
  • When you have several domain names pointing to one website but are only wishing to promote one domain name
  • When access to your website is possible via multiple URLs such as http://domain.com/ or http://www.domain.com. It is preferable to select one and 301 redirect the others to the one you have chosen. This is referred to as Canonicalisation.
  • Merging two websites together and want links to outdated URLs to be seamlessly redirected to the corresponding page on the new site.

Here is Google’s webmaster guideline on 301 redirects: http://www.google.com/support/webmasters/bin/answer.py?answer=93633

Apache Web Server

The Linux Operating system most commonly uses Apache web server. There are a number of methods of redirect available:

.htaccess Method

If you have sufficient access to the web server then the very best method of redirection is the creation and upload of an .htaccess file as this gives you the most flexibility and control:

Apache .htaccess Single Page Redirect
You will need to create a file named .htaccess (no extension), add the code below to the file using a text only editor and upload it to the root directory of your website:

RewriteEngine on
Redirect 301 /oldpage.html http://www.example.com/newpage.html

Apache .htaccess Canonical Redirect
Again you will need to create a file named .htaccess, add the code below to
the file using a text only editor and upload it to the root directory of your website. The code below will redirect all visitors accessing http://domain.com to http://www.domain.com:

RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]  

Control Panel Method

If you have Administrator access to the cPanel then this is another method of redirection but you may find that you are limited with the types of redirects that you can do, for example the Plesk control panel does not allow for 301 redirects.

cPanel redirect

Log into your cPanel, and look for “Redirects” under Site Management
Put in the current directory into the first box
Put the new directory in the second box
Choose the type (temporary or permanent) temporary=302 and permanent=301
Click “Add” and you’re done

On Page Redirects

In instances where you are only wishing to redirect one or two files and perhaps don’t have the server correctly configured for an .htaccess file or where you do not have sufficient access to the web server such as in a shared hosting environment, you can use on-page redirects.

Read more info on both PHP and ASP redirects.

Microsoft IIS Web Server

IIS supports the requirement for 301 redirection and all necessary changes can be made directly through the Control Panel. 

Administrator Mode

If you can log into the Windows 2000 (or higher) server and access the desktop, then this is the best method to carry out redirects in a Windows environment. The example below is based on the IIS 6.0 platform.

  • Choose Start / Setting / Control panel / Administrative Tools / Internet Information Services (IIS) Manager
  • Click the cross that is next to the computer to drop down the list of the configured folders within IIS.
  • Next you need to drop down the web sites folder; this will display all the website that have been configured in IIS.
  • Locate the domain that you wish to setup the 301 redirect on. When you have located this domain right click on it and select properties.
  • On the tabbed menu above click home directory. Once you are in the home directory pay close attention, you do not want to mess with any other settings this could result in your website not being displayed.
  • Select ‘A redirection to a URL’
  • In the redirect website box: Enter the website address that you want the site to direct to. Once you have done this, your website will forward but it still has not been set correctly, you must click a permanent redirection to; by checking this button you are telling the server that you want the redirection to be permanent.

On Page Redirects – See Below

On Page Redirects – More Information

PHP Single Page Redirect
In order to redirect a static page to a new address simply enter the code below inside the index.php file. This code must be located in a script that is executed on the server before the page content starts:

header(“HTTP/1.1 301 Moved Permanently”);
header(“Location: http://www.newdomain.com/page.html”);
exit();
?>

PHP Canonical Redirect
The code below will redirect all visitors accessing http://domain.com to http://www.domain.com. This code must be located in a script that is executed in every page on the server before the page content starts:

if (substr($_SERVER[‘HTTP_HOST’],0,3) != ‘www’) {
header(‘HTTP/1.1 301 Moved Permanently’);
header(‘Location: http://www.’.$_SERVER[‘HTTP_HOST’]
.$_SERVER[‘REQUEST_URI’]);
}
?>

ASP Single Page Redirect
This redirect method is used with the Active Server Pages platform. This code must be located in a script that is executed on the server before the page content starts:

<%
Response.Status=”301 Moved Permanently”
Response.AddHeader=’Location’,’http://www.new-url.com/’
%>

ASP Canonical Redirect
The code below will redirect all visitors accessing http://domain.com to http://www.domain.com. This code must be located in a script that is executed in every page on the server before the page content starts:

<%
If InStr(Request.ServerVariables(“SERVER_NAME”),”www”) = 0 Then
Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”,”<a href=”http://www/”>http://www</a>.”
& Request.ServerVariables(“HTTP_HOST”)
& Request.ServerVariables(“SCRIPT_NAME”)
End if
%>

These are some of the methods available to ensure you preserve your Internet footprint when updating your site content.

Disclaimer
All care has been taken in preparing the code in this blog, however no guarantee is offered as to the suitability of this code for your particular hosting environment. We suggest you copy the code into a text editor to remove all formatting. No responsibility is accepted for errors or omissions.

Related

Design & Development
And Now, How to Build the Perfect eCommerce Product Page
Design & Development
How to Build the Perfect eCommerce Product Category Page
Design & Development
Wildcard ‘*’ versus Exact Match ‘@’ DNS records
Design & Development
And Now, How to Build the Perfect eCommerce Product Page
Design & Development
How to Build the Perfect eCommerce Product Category Page
Design & Development
Wildcard ‘*’ versus Exact Match ‘@’ DNS records

Our thoughts

Let's collaborate • Let's collaborate • Let's collaborate • Let's collaborate • Let's collaborate • Let's collaborate • Let's collaborate • Let's collaborate • Let's collaborate • Let's collaborate • Let's collaborate

Like what you see?
Get in touch

"*" indicates required fields

Your personal information will only be used to service your enquiry. We will only contact you with relevant information. For further information view our full Privacy Policy.
This field is for validation purposes and should be left unchanged.

CONTACT

Hi, let's see how
we can help

"*" indicates required fields

Your personal information will only be used to service your enquiry. We will only contact you with relevant information. For further information view our full Privacy Policy.
This field is for validation purposes and should be left unchanged.