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 http://www.digimantra.com/tools/twitter/ and http://digimantra.com/tools/twitter/ 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 http://digimantra.com. 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 http://www.domain.com
RewriteEngine On
RewriteCond %{http_host} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
Code to be copied when your prefered domain is http://domain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^domain.com
RewriteRule (.*) http://domain.com/$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..
Cheers!!
Sachin Khosla




If I try to access your server at http://www.example.com:80 or at http://www.example.com./ (note the trailing period) your code will not issue a redirect, and the URL will be indexed as Duplicate Content.
How can that be fixed?
If you log in to Google Webmaster Tools (after authenticating your site), then you can choose there what you want it to show – with or without www.
PS – Install Subscribe to Comments plugin.
Well the plugin was there , i was acting too lazy to configure it. But its configured now. Thanks for the hola !!
Google Webmaster Tools, only sets your domain preference in its indexes, here is the image which explains you all
http://img193.imageshack.us/my.php?image=googlewebmasterwww.gif
my previous domain had preferred domain as http://realin.co.in and this one prefers to prefix http://www.digimantra.com and hence the difference in the google’s search indexes
Good post.. Is this what we call SEO?
Yeah its a part of SEO, it wont help you get to the top on a search engine index, but it can save you your site from getting marked as a duplicate content provider
Great.. may be beneficial for me later. In these days I couldn’t even manage time for posting content at my blog…
Hi sachin,
first of all sorry for previous post.
Thx for info but when I tried it for redirecting naked domain to www it gave me wierd result such as below.
http://example.com/abc/xyz/index.php gave me
http://www.example.com/xyz/index.php and
http://example.com/abc/index.php gave me
http://www.example.com/index.php
Pls look into it.
Thx
K.L.Deepak