Recently, it has been talked a lot about Google and duplicate content as well as Google Canonical problems.That is, when you have your site accessible both under your_domain.com and www.your_domain.com. To avoid such problems you can use the following lines in your .htaccess file to force only the www version of your web site:
RewriteEngine on RewriteCond %{HTTP_HOST} !^www.your_domain.com$ RewriteRule ^(.*)$ http://www.your_domain.com/$1 [R=301]
Please, note that the .htaccess should be located in the web site main folder.
This will redirect all requests to the non-www version of your site to the www version using 301 Permanent redirect which will make the search engines to index your site only using the www.your_domain.com URL. In this way you will avoid a duplicate content penalty.
I have read in some other article that Mod_rewriting is possible only on Linux based Apache server. My application is running on JBoss on windows platform So can i use .htaccess method to avoid www vs. non-www issue....
I see a lot of people telling how to do this fix & then also showing how to have the index.php page 301 to www.example.com, but not many people show how to combine both. Could you show an example of the htaccess file with both rules combined?
Okay, so I have two subdomains, as well as the main site on www.
But Google is listing lots of non-www random character subdomains.
I can implement the .htaccess commands fine, moving non-www to www, but won't they also divert my two legitimate subdomains? How do I exempt those from being redirected?
Actually Robert is not quite correct. I run Apache on Windows XP for development/testing with the functionality oh .htaccess with no problems.
Windows does not like a filename to start with a dot so attempting to create .htaccess form explorer causes an error. Programmatically it is possible to create .htaccess but you don't have to go to that trouble.
Apache's httpd.conf has a line:
AccessFileName .htaccess
Just change that to read
AccessFileName htaccess
Them create your .htaccess rules in htaccess and it will work without problem.
By the way thanks for putting this information up on the web. I spend weeks looking for a site like this and now my search is over.