Ratings, reviews, plans and features to help you find the right web hosting provider for your site.

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 64 >>

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
Tom Said,
Nov 02, 2008 @ 12:38

Really useful! Thanks a lot!
Titan Said,
Jan 11, 2009 @ 16:02

all can be solved by the Google webmaster tools. you can choose to present your www or non www version so even if your incoming link has no www, Google sees it as if you have it.
George Said,
Feb 04, 2009 @ 23:43

very useful, thanks
otop Said,
Feb 23, 2009 @ 21:25

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

Is this the lastest version ?

thx a lot
Canadian Web Designer Said,
Apr 09, 2009 @ 08:49

Thanks for the 301 redirect! This will definitely help with SEO - I've already set up my accounts (through a panel) to redirect, but it's good to know how to do it so that I can help my clients set it up on their servers (if the web design or application I make doesn't get hosted on mine).
Build Muscle Said,
Apr 21, 2009 @ 13:32

Thanks for this. I always forget the rule when I'm working randomly on an .htaccess file, or when I've giving advice with email. Thanks to your page and a bit of copy and paste, I now have the answer quickly. Thanks again!
Nicholas Maietta Said,
May 16, 2009 @ 15:12

There seems to be a little confusion on this subject. For those who this does not work for, you have to be sure that your host/webserver doesn't specifically block Mod_Rewrite directives in apache. Second, I'd suggest that the purpose of this particular rewrite rule be given an example reason besides the use of Google's duplicate content problem. Third, I'd like to remind folks that webmasters can create a google webmaster account and then make changes to how google crawls and indexes your your site's content. Changing your site to suit one or two search engines isn't the best way, when those search engines provide tools to tweak how they manage and display information about your site.
Martin Lindberg - kommunikationsbureauet KOMMID Said,
Jul 31, 2009 @ 04:29

Finaly!
On my Linus based server this didn´t work:

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

But this did:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^maindomain.com
RewriteRule ^(.*)$ http://www.maindomain.com/$1 [R=301]
Free Ads, Online Classifieds Said,
Sep 02, 2009 @ 22:13

It worked for me too as described above

But you need to place the file rewrite.properties in the right folder typically in the folder jboss.web under conf
Nicholas Maietta Said,
Oct 01, 2009 @ 00:00

I've read the comments here, and there seems to be a lot of people who do not yet have a solid or even loose understanding of what .htaccess is.

The . (dot) designater has a long history on web servers. The .htaccess file is simply a text file that may contain Apache directives. FTP servers often are configured not to list these .htaccess and .* files.

Apache directives are important for webmasters to learn. All HTTP requests to an apache based web server get handled after passing the apache directives... the "filter" or directions, if you will.

Most of the web server's configuration is already done in the apache config files. It is up to the server admin to define if a "virtual" host can use apache directives to override default settings for web hosting.

To learn more about apache directives, google it.
free poker bankroll Said,
Oct 09, 2009 @ 03:30

Hy,

I see that everyone discuss about Apache Servers and .htaccess file. My domain is hosted on a windows server and can't use a .htaccess file. What should i do to redirect my non www version to www version of my website? I read many articles and forums but i don't understand how to do. I need step by step instructions.
Thanks in advance!
bill Said,
Nov 10, 2009 @ 18:09

I am new to this, but I want to create a .htaccess file that will redirect domain.com to www.domain.com and also index.html to www.domain.com. I put the code below in i .htaccess file, it did not work. It works fine with just the first 3 lines, but not when I add last 3 lines. Please advice..

RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} index.html
RewriteRule .* http://www.mydomain.com/? [R=301,L]
Michael Howey Said,
Dec 23, 2009 @ 22:11

Easy way to fix this problem

$mydomain = "myrealdomain.com";

if($_SERVER["HTTP_HOST"] == "$mydomain") {
$change_url = "http://www." . $mydomain. $_SERVER["REQUEST_URI"];;
header("Location: $change_url");
exit();
}


and if you have more domains and you want them directed to your main domain you could do something like this.


if($_SERVER["HTTP_HOST"] == "otherdomain.com") {
$change_page = "http://www." . $maindomain . $_SERVER["REQUEST_URI"];
header("Location: $change_page");
exit();
}
elseif ($_SERVER["HTTP_HOST"] == "www.otherdomain.com") {
$change_url = "http://www." . $maindomain . $_SERVER["REQUEST_URI"];
header("Location: $change_url");
exit();
}
diseño web Said,
Jan 22, 2010 @ 15:54

thanks, served much to me for a dominion badly formed
Energie Blogger Said,
Mar 07, 2010 @ 14:40

Thank you for that . Now i force non www . Its the pure url in my eyes !

Ty
r8r Said,
May 10, 2010 @ 07:26

if you want to process more rules (for example using the zend_router of the zend framework) make sure to add the [L] flag to the rule!

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.your_domain.com$
RewriteRule ^(.*)$ http://www.your_domain.com/$1 [R=301,L]
Jill Said,
Jul 19, 2010 @ 03:26

My site have a dedicated IP. I want that if somebody type my ip my site should not show up and he should be taken to the www version of the site. This should also happen with other suppages not just with index page.
Regards
vince Said,
Jul 21, 2010 @ 01:21

Hi Jill,

The above rule should take care of your problem. It simply reads:

If the requested HOST differs from www.domain.com, redirect the user to www.domain.com - that is, if your site is accessed by IP, the rules will redirect to www.domain.com.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.your_domain.com$
RewriteRule ^(.*)$ http://www.your_domain.com/$1 [R=301,L]
Jill Said,
Jul 21, 2010 @ 16:22

Hello Vince,
I am impressed of this solution but this is working for index page only. If i enter IP address/Directoryname then it redirects me to index page where as i want that this should redirect me to http://www.sitename.com/Directoryname .
Please help
vince Said,
Jul 22, 2010 @ 03:56

It should be working just fine, may be it is something specific to your site.

Do you have other rules in your .htaccess file?

Do you have other .htaccess files in your "Directoryname" folder that might be interfering with the rules above?
James Said,
Jul 25, 2010 @ 09:21

I'm forcing non-www with this:

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

Georgi Iliev above take note, your version does not remember the page the visitor is on, but always sends them back to the home page if they include www. Not cool!
sekarningrum Said,
Oct 04, 2010 @ 19:35

The following link will be helpful to have a good idea. Remember " Please, note that the .htaccess should be located in the web site main folder." Great works, Verywell thank U
veggera Said,
Oct 07, 2010 @ 06:32

Hello
both great tips for .html VS /
and .www to non www ..
but i come to late to fix this ,and this mornig i find 2.546 duplicate title tags
and 3.568 duplicate meta discription ..

how to (clean) this from G.W.M. Tools
Thanks
Richard Said,
Nov 05, 2010 @ 22:12

For forcing the removal of www.

What is the code for wildcard subdomains like on a wordpress MU? (and do you even need it for a WPMU site?)

Thanks
Bob Said,
Nov 25, 2010 @ 22:36

Hi

How to fix www and non www duplicate content for addon domain. Is it similar to park domain or main domain that discussed above.
System Analyst Said,
Dec 23, 2010 @ 15:52

Thanks for the .htaccess information, it helped me in putting 301 redirect to avoid duplicate info.
Christopher Vrooman Said,
Jan 08, 2011 @ 14:46

How about redirecting but with support for httpS?

Most of the above will redirect a request for example.com to http://www.example.com, but what if the request was for https://example.com? Wouldn't you still be redirected to http://www.example.com which negates the use of the SSL certificate.

I found this code on stackoverflow: http://stackoverflow.com/questions/2097542/use-htaccess-to-add-www-with-https-support, but I still get an Error 404 when trying it on my WordPress installation.

Any alternative solutions?
Vince Said,
Jan 08, 2011 @ 16:04

@ Christopher Vrooman

You can try the following, it should be working just fine:

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

It combines the example here with the StackOverflow one.
Jack Said,
Jan 16, 2011 @ 14:33

Here is the proper way of doing it to avoid re-directing subdomains as well.

http://www.theblogaholic.com/2011/01/16/force-www-using-htaccess-except-for-subdomains/
Joe Said,
Jan 27, 2011 @ 01:25

Hi, I did the 301 re-direct, and now I am showing hundreds less incoming links. I used to have more than 500, and now it says I have 50? Is this because of the re-direct?

Am I way off?

Thanks for this article!
kash Said,
Jan 28, 2011 @ 13:45

how do you check the incommings links?
kuko Said,
Mar 07, 2011 @ 10:00

@Michael Howey
perhaps you would like to do something with redirect:
header( "Location:url", TRUE, 301 );
// header( 'HTTP/1.1 301 Moved Permanently');
kevinrose Said,
Mar 12, 2011 @ 23:23

This can be done by PHP script also. Include following code on top of all page.



This code is working for me.
kevinrose Said,
Mar 13, 2011 @ 03:28

in above post code is missing. i put the code with php tag it may be the reason. following is the code. for force www redirect.

$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://www.";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}

if (!preg_match("/www/i", $_SERVER["SERVER_NAME"])) {
header('location:'.$pageURL);
die;
}
Mike Said,
Mar 19, 2011 @ 21:46

Hi, my main website is in the public_html folder and i want to move it into a subfolder for example sites/mywebsite/

I want to make a permanent (301) redirection to this subfolder and the browser retains the original address. For example if someone visits either of these two urls :
a) http://example.com/ and
b) http://www.example.com/
I want to be redirected to http://www.example.com/sites/mywebsite/
and the url should stay http://www.example.com/

I tried the following:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/sites/mywebsite/$1 [R=301,L]

When I visit http://example.com/ the redirection succeeds but the url changes to http://www.example.com/sites/mywebsite/

When I visit http://www.example.com/ no redirection is made an the browser shows the directory structure of the public_html folder.

Please help solving this issue. Thank you
vivaystn Said,
Mar 30, 2011 @ 22:02

try to clear the cash to see the changes
izdelava internetne trgovine Said,
Jun 21, 2011 @ 05:37

Nice solution, it works like a charm. I hope Google will now like my site better :)
Mike Said,
Jun 23, 2011 @ 00:19

What is the difference between [R=301] and [R=301,L]?
Seo India Said,
Jul 04, 2011 @ 08:29

this is what i am exactly looking for. this code works for me.

bunch of thanks.
dell Said,
Aug 04, 2011 @ 05:33

thanks its nice great site and i have added in my favorites list
bobby Said,
Aug 08, 2011 @ 14:01

godaddy windows hosting .htaccess do not work, any other option? asp, java script? etc. thx
Jay Bee Said,
Aug 16, 2011 @ 15:23

I manage www.nolacarpetcleaning.com my web designer reworked my site and now it just shows up http://nolacarpetcleaning.com .... Is this hurting my rankings?
SEO Company India Said,
Aug 17, 2011 @ 09:03

Can any one have .htacces file which working fine if have tell me please bcoz i alos want to setup 301 redirect on my website if anyone have so mail me on my revert me back...
mocua.com Said,
Oct 01, 2011 @ 19:45

Thanks and thanks
John Robinson Said,
Oct 04, 2011 @ 11:15

Thanks for this post, really helpful
Vic Said,
Nov 28, 2011 @ 11:57

I'm trying to force www to non - as well, I want to do this for https.

Using the code below:

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


Now, how do I keep my subdomains from redirecting to my main domain?

e.g. maindomain.com/subdomain.com

Should I do this in the main domain?
If so, how do I add them to the above code.

Also, can I add the .htaccess with the above code in each subdomain root and achieve the same thing?

Thanks for great info and to everyone else as well.
Kim Said,
Dec 02, 2011 @ 03:06

Hi I have an old site I need to add a .htaccess file to. I have the main domain and a second domain parked. All links are relative. All new sites I do have all links absolute - good SEO, but this is an older site and I need to add a 301 redirect for the index.html to go to the www.maindomain.com.au/
This does work except for the index.html. Need advice the server is Apache. Thanks in advance.


RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.secdomain.com.au$ [OR]
RewriteCond %{HTTP_HOST} ^secdomain.com.au$ [OR]
RewriteCond %{HTTP_HOST} ^maindomain.com.au/index.html$ [OR]
RewriteCond %{HTTP_HOST} ^www.maindomain.com.au/index.html$ [OR]
RewriteCond %{HTTP_HOST} ^maindomain.com.au$
RewriteRule ^(.*)$ http://www.maindomain.com.au/$1 [R=301,L]
Kim Said,
Dec 02, 2011 @ 07:13

Hi again, yes I realise this is for domains not pages. Sorry. I'll change all my home links to absolute and delete the two lines referencing the index.html.
Still learning...
Thanks.
mikeyh Said,
Dec 21, 2011 @ 06:16

We use Helicon's "isapi rewrite" plugin for IIS to enable htaccess goodness on Windows hosting.

My problem related to a client with multiple domain names that needed to all be pushed on to the same domain.

The code in the original post at the top of the page worked perfectly to achieve this. Many thanks :)
seo zooms Said,
Jan 11, 2012 @ 05:19

I have website hosted in windows server. The .htacces file is not working. Please somebody help me how I can solve www and without www version of my website?
Your comments on this article

(required)

(required but never displayed)



security code



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