web hosting reviews


Force www vs non-www to avoid duplicate content on Google

Web Hosting Articles \ A simple guide to .htaccess \ Force www vs non-www to avoid duplicate content on Google


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.


  1. How to block users from accessing your site based on their IP address
  2. How to prevent or allow directory listing?
  3. How to change the error documents – 404 Page Not Found, etc
  4. Using .htaccess for password protecting your folders
  5. Using .htaccess to block referrer spam
  6. Disable Hot-Linking of images and other files
  7. Redirect URLs using .htaccess
  8. Introduction to mod_rewrite and some basic examples
  9. Force SSL/https using .htaccess and mod_rewrite
  10. 301 Permanent redirects for parked domain names
  11. Enable CGI, SSI with .htaccess
  12. How to add Mime-Types using .htaccess
  13. Change default directory page
  14. Block Bad robots, spiders, crawlers and harvesters
  15. Make PHP to work in your HTML files with .htacess
  16. Change PHP variables using .htaccess
  17. HTTP Authentication with PHP running as CGI/SuExec
  18. Force www vs non-www to avoid duplicate content on Google
  19. Duplicate content fix index.html vs / (slash only)

Comments 14 >>

Robert Said,
Aug 03, 2006 @ 18:18

How can I get parked domain names redirect and also force non-www to www at the same time.

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.
vince Said,
Aug 04, 2006 @ 03:56

For parked domains (aliases of the main domain) just use:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.maindomain.com$
RewriteRule ^(.*)$ http://www.maindomain.com/$1 [R=301]

This will always redirect to the www version of the main domain.

Hope this helps
Robert Said,
Aug 07, 2006 @ 12:36

That did not work for me but below is how I got it to work.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^2nd-domain.ca [OR]
RewriteCond %{HTTP_HOST} ^www.2nd-domain.ca [OR]
RewriteCond %{HTTP_HOST} ^maindomain.com
RewriteRule ^(.*)$ http://www.maindomain.com/$1 [R=301]
sharad Said,
Feb 01, 2007 @ 06:39

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....
vince Said,
Feb 03, 2007 @ 20:17

Yes, Mod_Rewrite is a feature of Apache.

I am not sure what the Jbos feautures are you may need to contact your system administrators about that.
Mark Said,
Jul 26, 2007 @ 12:37

I had the same problem on my site. I'm glad I use Apache I remember in the old days iis didn't do any rewriting.
Georgi Iliev Said,
Nov 08, 2007 @ 18:31

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule (.*) http://example.com/ [R=301,L]

redirects the www version to the non-www one ... useful for some people (like me for example:))
typo3 hamburg Said,
Jan 08, 2008 @ 15:13

Hi there,
inside of typo3 you need only the list-element "domain" in the top of the websitetree... you can run the standard .htaccess from typo3.

forward everything without "www" including the parameters.
Jeff Said,
Feb 10, 2008 @ 02:57

Great resource!

I found your site on Freekat's Knowledgebase...

The page is: http://www.freekatsearch.com/tools/knowledgebase/force-www-in-htaccess.html

All of this talk about google is making me nauseous.
Bai Said,
Feb 27, 2008 @ 14:42

Does .htaccess not work on a window platform?

i upload the file to the web and when i type in legsource.com; it still says: http://legsource.com

i have another domain; but it's on a unix and it works.

thanks
robert Said,
Feb 29, 2008 @ 16:48

noop. .htaccess work on unix or linux platform
Vacation Rentals By Owner Said,
Mar 06, 2008 @ 23:47

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?

Would you mind emailing me if you update this?

Craig
Michael Said,
Apr 18, 2008 @ 00:58

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?
Nick Kotarski Said,
Jun 09, 2008 @ 09:26

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.

Nick
Your comments on this article

(required)

(required but never displayed)



security code



Next: Duplicate content fix index.html vs / (slash only)

Back to Web Hosting Articles list

Web hosting companies A-Z:
All web hosting companies


About - Terms of Use - Privacy Policy -
Web Hosting Reviews