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.
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:
Once these are uncommented and you have ensured the conf file syntax is correct, then restart apache:
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
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.
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
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.
<?php
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.new-web-address.com" );
?>
<.cfheader statuscode="301" statustext="Moved permanently">
<.cfheader name="Location" value="http://www.new-web-address.com">
<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www.new-web-address.com/"
%>
<%
response.setStatus(301);
response.setHeader( "Location", "http://www.new-web-address.com/" );
response.setHeader( "Connection", "close" );
%>
def old_action
headers["Status"] = "301 Moved Permanently"
redirect_to "http://www.new-web-address.com/"
end
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.