Hochman Consultants - Results Driven Internet Marketing
Client Login

Code for URL Canonicalization with 301 Redirects - .htaccess, ISAPI_rewrite ASP, PHP

Recommendations:
Apache Logo
Apache servers
use mod_rewrite

Windows Logo

Microsoft Windows
Internet Information
Services (IIS) servers
use ISAPI_rewrite

Do you have a portfolio of multiple domain names that all lead to the same web site? Valid reasons for doing this include helping visitors who may type a domain name with spelling mistakes, hyphens, no hyphens or confuse different names, as well as preventing competitors and typosquatters from using a similar domain name. For a few dollars per year per domain name, it often makes sense to buy all the obvious mistakes and permutations of your valuable domains.

When you attach multiple domain names to a site, search engines can become confused and your rankings may suffer. If they find the same page at two or more different URLs, the search engines will sometimes filter out the extra listings, but there's no guarantee of which ones they filter. You also don't want people linking to your site with non-standard domain names because that will tend to divide your inbound link strength.

Why leave it to chance? With a few lines of code you can make sure all your URLs use consistent domain names. When we have multiple domain names on one site, we set up a permanent 301 redirect for any extra domains, as well as the www subdomain or the non-www domain. If you use any of this code, be sure to replace my domain name with yours, or else you'll be sending me all your traffic.

If you have an Apache web server, add the following code to a .htaccess file in the top level directory:

Options +FollowSymlinks
RewriteEngine on
#Don't redirect https pages
RewriteCode %{SERVER_PORT} ^80$  
RewriteCond %{HTTP_HOST} !^www\.hochmanconsultants\.com$
RewriteRule ^(.*) http://www.hochmanconsultants.com/$1 [L,R=301]

If you are using a Microsoft IIS web server, I recommend using ISAPI_rewrite to simulate Apache's .htaccess feature. To begin, you or your hosting provider needs to install ISAPI_rewrite on the server. Then you would create a httpd.ini file in the top level directory as follows, replacing the test domain name with yours:

RewriteCond Host: (?!^www\.hochmanconsultants\.com$).*
RewriteRule (.+) http\://www.hochmanconsultants.com$1 [RP,I]

If you have an IIS server and are using ASP scripting, the following code will fix most URL canonicalization issues.

<%
if (Request.ServerVariables("server_name") <> "www.hochmanconsultants.com") Then
	dim url
	url = Request.ServerVariables("URL")
	if url = "/index.asp" Then
	 	url = "/"
	End If
	Response.Status="301 Moved Permanently" 
	Response.AddHeader "Location","http://www.hochmanconsultants.com" & url
End If
%>

URL canonicalization can also be set up with PHP code, such as the following example.

<?php
if ($_SERVER['HTTP_HOST'] != "www.hochmanconsultants.com") {
	Header( "HTTP/1.1 301 Moved Permanently" ); 
	$script_name=str_replace("index.php", "",  $_SERVER['SCRIPT_NAME']);
	Header( "Location: http://www.hochmanconsultants.com".$script_name ); 
}
?>

If you have an IIS server, but don't have ISAPI_rewrite, nor ASP scripting, Ian McAnerin has published a very helpful article that explains how the server administrator can set up permanent 301 redirects on IIS .

These solutions assume you prefer to use the www subdomain for your web site. If you would rather have a non-www URL, simply remove each instance of "www\." and "www." from the rewrite conditions and rules. As with all code, please test thoroughly before deploying to your live server. Your website may require different code from the examples presented here.

About the Author

After graduating from Yale with two degrees in Computer Science, Jonathan Hochman set up his own consulting company in 1990. He has been an Internet marketer since 1994. To send feedback, please visit http://www.hochmanconsultants.com/.

Creative Commons License You may distribute this article in its entirety under a Creative Commons Attribution 3.0 License. Please attribute the author by name and link (URL).

For additional information, please contact Hochman Consultants.