First of all you need to know why you even want to prefix www in front of your domain. Well to explain in short, search engines treat and differently. Notice the difference is just of www. But  if you have these two URLs distributed all over the web, then search engines may treat your content as duplicate. So to avoid duplication of content we redirect the users and search engines to the prefered domain just like I chose www over While redirecting we also need to send a redirect code 301 so that the search engines update their indexes ASAP.

Well the theory part is over and here is the code which you need to copy paste to the .htaccess file which should be present in your root folder. This rule is independent of any sorta wordpress or CMS install. But please make sure you make the changes after taking backup, because if you set wrong rules or conditions in .htaccess then you may end up messing up your site’s URL structures and hence loss of visitors.

Code to be copied when your prefered domain is

RewriteEngine On
RewriteCond %{http_host} ^ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

Code to be copied when your prefered domain is

RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^domain\.com
RewriteRule (.*)$1 [R=301, L]

Hope this helps anyone, there is also a PHP solution available, its easy but I really do not want visitors to go for it, but still in case you do not have access to .htaccess or something, lemme know i can write it down in comment later..

Sachin Khosla

Share this post: