Home > Apache, Linux, seo, Web > Using htaccess to redirect domain to www

Using htaccess to redirect domain to www

Welcome back!!

Today, I’ll show you an easy rule to redirect domain.com to www.domain.com to help you prevent your site from duplicate content. I know it has been said thousand of time on other site about it, but I still meet persons/companies that doesn’t properly configure their address…

First of all, this rule only work for apache webserver having the “AllowOverride” set to something else than “none” and having the “mod_rewrite” module enabled.

You may ask to yourself, do I need such a rule for my site? This is how to find out if you do.

1) Call your site using http://domain.com/, see your home (or see 404)

2) Call your site using http://www.domain.com/, see your home

If the page load without any redirect (eg, to change domain.com to www.domain.com), then YES, you need this rule.

If you see, your home on both address, then you might be in duplicate content.

If you see a 404 on one of the 2 pages, then visitors might hate you for forcing them to add “www” to access your site (instead of simply calling domain.com)

Here is the rule to write in the .htaccess file, which should be place at the root folder of your site directory:

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

Make sure you replace “domain.com” on the 2 lines above with your own site domain.

Enjoy :)


			
Categories: Apache, Linux, seo, Web Tags: ,
  1. No comments yet.
  1. No trackbacks yet.
*