How to Stay Safe from Duplicate Content by Using .htaccess and 301 Redirects

Content/SEO

How to Stay Safe from Duplicate Content by Using .htaccess and 301 Redirects

It’s not uncommon for a site to have mutiple domains and sub-domains pointing to the same content.  For example, you can get to this site using sitening.com or www.sitening.com.  But for on-site SEO it is important that all requests are 301 redirected to a single hostname.  If Google can get the same content by going to two different hostnames, then their algorithm may split the PageRank between those two sites, and at worst the site could become delisted from the index.  Not good.  Luckily the fix is easy if your site is running on an Apache server. The first step is to create an htaccess file (plain text document) and save it as .htaccess in the root folder of the website. Then add the following lines of code (replacing sitening.com with your own domain.)

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^sitening.com
RewriteRule (.*) http://sitening.com/$1 [R=301,L]

The first line turns on mod_rewrite, which is an Apache module that’s used to rewrite URLs.  The second line is a personal preference that ignores the first slash from the URLs, which makes the rest of the commands a little more readable.

The RewriteCond line translates it into a condition: "If the hostname does not start with sitening.com."  Hostname is the part of the URL between http:// and the first slash. You can have as many conditions – lines that start with RewriteCond – as you need.

If you develop your sites on separate servers, you might add this condition so it accepts our development server hostnames:

RewriteCond %{HTTP_HOST} !^sitening.site

The last line that starts with RewriteRule translates into an action: "Take the entire requested page (.*), minus the first slash, and append it to a new url like this: http://sitening.com/$1.  Then send a redirect to the browser with the new url, and use HTTP status 301 (R=301), and stop processing rewrite rules for this request (L)."

Using the HTTP status of 301 is important because it indicates that the new URL is permanent.  This tells Google to update its index with the new URL, thereby attributing all of the PageRank to the new hostname.

You can tell if it’s working by doing site: searches on Google for all of the hostnames that point to your site.  If you search for site:sitening.com, you’ll see all of our web pages indexed.  If you search for site:www.sitening.com, you should see no pages.  Try this on your own site to see if your PageRank is divided across multiple hostnames. Please note, it may take several weeks for the redirected hostnames to be removed from a search engine’s index, so be patient.

Get the source code at Snipplr: Redirect to main domain

Comments are closed on this post