Multiple Domains: SEO with 301 Redirects




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.

Valid reasons for having more than one domain name attached to the same website 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 a primary domain.

Google recommends using 301 redirects to standardize URLs. A few lines of code can ensure that alternative URLs consistently lead to your preferred URL, maximizing the potential for search engine appearances. Best practice is to set up permanent 301 redirects for any extra domains, as well as the www subdomain or the non-www domain.

To find the right code, start by identifying what type of server you have. If you don’t know, use https://urivalet.com/ to inspect your server headers and identify your server type. Common server types include Apache, nginx, and Windows IIS.

Apache and nginx servers – Best Solution

If you have an Apache or nginx web server that supports mod_rewrite, add the following code to a .htaccess file in the top level directory. When using this code, be sure to replace the test.com domain name with your domain name. If your site uses ‘https’ you will need to change ^80$ to ^443$, since https usually runs on port 443.

The www version:

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
#Don't redirect https pages
RewriteCond %{SERVER_PORT} ^80$  
RewriteCond %{HTTP_HOST} !^www\.test\.com$
RewriteRule ^ http://www.test.com%{REQUEST_URI} [L,R=301]
</IfModule>

Non-www version:

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
#Don't redirect https pages
RewriteCond %{SERVER_PORT} ^80$  
RewriteCond %{HTTP_HOST} !^test\.com$
RewriteRule ^ http://test.com%{REQUEST_URI} [L,R=301]
</IfModule>

Microsoft IIS Servers – Best Solution

If you are using a Microsoft IIS web server, use 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\.test\.com$).*
RewriteRule (.+) http\://www.test.com$1 [RP,I]

ASP Classic – Alternative Solution for IIS

If you have an IIS server and cannot install ISAPI_rewrite, but have ASP classic scripting available, the following code will fix most URL canonicalization issues. Place the code at the top of each page before the <html> tag, and be sure to replace test.com with your domain name.

<%
if (Request.ServerVariables("server_name") <> "www.test.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.test.com" & url
End If
%>

PHP – Alternative Solution for Apache, nginx, IIS

If your server does not support .htaccess, nor ISAPI_rewrite, but does have PHP, the following code may be used. Place the code at the top of each page before the <html> tag, and be sure to replace test.com with your domain name.

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

IIS Alternative Solution

If you have an IIS server, but don’t have ISAPI_rewrite, ASP Classic, 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 than 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.

For additional information, please contact Hochman Consultants.