Improve Your Website SEO by Stopping Duplicate Content

November 5th, 2011 | Posted by hhanifl in SEO & Internet Marketing

Most websites can be accessed with either www.domain.com and domain.com. Since Google penalizes this due to duplicated content reasons, you have to stick your domain to either www.domain.com or domain.com.

To prevent this from happening on an Apache server use a 301 redirect for all http requests that are going to the wrong url.

Redirect #1 – Redirect domain.com to www.domain.com

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

Redirect #2 – Redirect www.domain.com to domain.com

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

Explanation of the .htaccess 301 redirect

You only really need to read this if you want a explination of what the codes does, if you aren’t server savvy or have no care to be, just skip the rest as the magic is already done. Redirect domain.com to www.domain.com. The first two lines just tell Apache to handle the current directory and start the rewrite module.

Next RewriteCond %{HTTP_HOST} !^www.domain.com$ indicates that the next rule only gets run when the http host is not (- specified with the “!”) www.domain.com. The ^ means that the host starts with www.domain.com, the $ means that the host ends with www.domain.com – and the result is that only the host www.domain.com will trigger the following rewrite rule. Combined with the inversive “!” is the result every host that is not www.domain.com will be redirected to this domain. The [NC] specifies that the http host is case insensitive.
The escapes the “.” – becaues this is a special character (normally, the dot (.) means that one character is unspecified).

Next  sets the action to be executed: RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]. Next statement http://www.domain.com/$1 sets the target of the rewrite rule, where $1 contains the content of the (.*). Next we set the 301 redirect to run automatically: [L,R=301]. L means in this is the last rule in this run – so after this rewrite the webserver will return a result. The R=301 means that the webserver returns a 301 moved permanently to the requesting browser or search engine.

 

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>