How To Setup 301 Redirects for a Page or Entire Domain
301 Redirects are a big part of SEO and making sure that (A) the search engine bots find your content correctly, and (B) understand that the old page or domain is the same as the new one and thus combine the overall rank statistics to the one location. In essence the 301 redirect simply issues a Permanently Moved message in the HTTP header which tells the search engine to only index the target URL.
Examples of when you need a 301 redirect to help transfer your Google Juice include: when you have moved a content’s URL from one location to another, when you are disambiguating content in your database and combining the 2 separate URLs into one, and then you have changed domains, or have been running two domains in tandem and want to turn them into one.
301s Using the Apache Web Server
Getting Started – Enabling The mod_rewrite Engine
Whether you are using shared web hosting or you have your own dedicated box, the chances are that the mod rewrite engine is already installed and running. That being said, if not, then you need to edit the /etc/httpd/conf/httpd.conf file, removing the # before the following lines, and thus making sure they are uncommented before restarting:
- # LoadModule rewrite_module modules/mod_rewrite.so
- # ClearModuleList
- # AddModule mod_rewrite.c
Once these are uncommented and you have ensured the conf file syntax is correct, then restart apache:
- $ apachectl restart
Redirecting A Single Web Page or Web Address
If you have moved a page form one directory to another and you want the content to be updated in the search engine index, but without losing any of that all important and hard gained search engine robot crawl juice, you need to do the following in the html root directory .htaccess file:
RewriteEngine On Redirect 301 /old-dir/old-page.html http://www.domain.com/new-location/new-page.php
Redirecting to Fix Canonical Issues with the URL
If you prefer to refer to, or display your site as www.domain.com rather than domain.com to your sites visitors then you need to setup the .htaccess file in your html root directory as follows:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
In addition, to ensure that search engines such as Google only index the right canonical name and display the same name in search results is to setup an account with their webmaster tools and to specify that you prefer to use the website hostname with or without the WWW.
Redirecting An Entire Domain or Web Site
If you recently moved or changed domains and the content on one domain is identical to the other but it is that which you wish to show up in the search engine index instead, simple set up a domain redirect from domainA.com to domainB.com. You would do this in the .htaccess file as follows:
redirectMatch 301 ^(.*)$ http://www.domainB.com redirectMatch permanent ^(.*)$ http://www.domainB.com
Redirecting in Code: PHP, ColdFusion, ASP, Ruby on Rails, JSP, Java, Perl
If you don’t wish to peform the 301 redirect at the server level and just use the codebase to redirect site visitors and spiders accordingly, then you can easily do the same in any number of different web programming languages. Here are a few examples of 301 redirects in PHP, ColdFusion, ASP, Ruby on Rails, JSP, Java and Perl.
The PHP 301 Redirect
<?php
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.new-web-address.com" );
?>
The ColdFusion 301 Redirect
<.cfheader statuscode="301" statustext="Moved permanently"> <.cfheader name="Location" value="http://www.new-web-address.com">
The ASP 301 Redirect
<%@ Language=VBScript %> <% Response.Status="301 Moved Permanently" Response.AddHeader "Location","http://www.new-web-address.com/" %>
The Java / JSP 301 Redirect
<%
response.setStatus(301);
response.setHeader( "Location", "http://www.new-web-address.com/" );
response.setHeader( "Connection", "close" );
%>
The Ruby on Rails 301 Redirect
def old_action headers["Status"] = "301 Moved Permanently" redirect_to "http://www.new-web-address.com/" end
Conclusion: A Bunch of Different 301 Redirect Ways
As you can see there are a range of different ways to perform a 301 redirect and some may be more easy to implement for you than others, whether you are moving your blog away from wordpress to your own domain, or if you want to redirect search engines to newly moved content, or just redirect an entire domain. Whatever happens it’s possible to acheive your goal and you know how now!
Find out more about mod_rewrite for Apache over at the Apache Module mod_rewrite docs.
