Using htaccess to ensure correct site domain name & redirect

If you’re lucky enough to have more than one extension of the same name domain name (.com and .co.uk for example), or even if you don’t, but have multiple domain names pointing to the same site, then you will almost certainly benefit to redirect all traffic to just one domain name.

Continuing with the SEO side of things, Google & other search engines out there aren’t too keen on duplicate content, as I’m sure you’re aware of. If you’re running with more than one domain name and they’re both pointing to the same patch on the Internet, then there’s a good chance Google will see this as Dup Content.

Even if you’re only running with one domain, you may notice you can type in http://domain.com and http://www.domain.com and still get to the same thing (in most cases). Even this might be seen as another site, and therefore possible Dup Content. There’s an easy fix, however…

After some searching about to get a chunk of code which worked out of the box, this bit of htaccess code does the trick perfectly. Simply create a file named .htaccess in the root of your web directory, and paste in the following code:

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

In simple terms, the above script is basically saying, if the domain name is not www.domain.com, then redirect the page requested to http://www.domain.com with a 301-header permanent redirect on it.

So, for example, if you were to visit http://domain.com/page.html, then you’d be redirected to http://www.domain.com/page.html. Perfect!

 

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>