No WWW Using .htaccess

Written by David Walsh on Tuesday, January 8, 2008


There are some clear benefits to removing the “www” from your URLs:

  • Shorter URLs
  • Prevention of Google search engine penalties for duplicate content
  • Less bandwidth usage if you have many links per page

Luckily, removing the WWW from your page URLs (and subsequent Google listings) can be done inside your .htaccess file so that you wont have to go into every page on your website and remove the “www” from your anchors.

The .htaccess Code

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

Conversely, if you’d prefer to use “www” in all of your URLs, you can code:

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

Follow via RSS Epic Discussion

Commenter Avatar January 08 / #
Martin says:

Great tip! But what if I have several domains? This only works with one domain.

Commenter Avatar January 08 / #

@Martin:

This can work with multiple domains. Just change the domain name in the code. I use this same htaccess on all of my sites. It is an absolute requirement for my sites.

@David:

Just so you know the standard way to do it is to ADD the www. to domains and take away the www. on subdomains. Just in case you didn’t know the norm.

As far as shorter urls, it really doesn’t matter because most people will just type the domain without the www. but it will be redirected to the www.

Google now has the option in the sitemaps tool to specify which version of the domain you want Google to favor over the other. I recommend setting this first before changing the htaccess if you have a page that is already indexed and ranked.

Commenter Avatar January 08 / #
Shawn says:

I usually set it to add the www. I know the double-u, double-u, double-u, dot get’s redundant and really isn’t needed in todays web world, but it’s just a finicky decision on my part. :p

Commenter Avatar January 17 / #
Martin says:

@Mark:
Yeah, off course…but I meant if I have several domains to one web site. For example, in Sweden we are strange and have three extra letters (å, ä and ö) and if I have a name with any of these in I have to get one address with the swedish letter and one with a or o instead.

I know that I’m supposed to change the address in the example to my domain address.

Commenter Avatar January 17 / #
Leafnode says:

Using RewriteEngine to do this is like using a cannon to shoot a fly. Much “lighter” solution is to make a dummy virtual host and redirect every request using RedirectMatch, like:

ServerName http://www.example.com
RedirectMatch 301 (.*)$ http://example.com$1

Or, even lighter, you may use simple redirect to main page (no regexps), but it’d be confusing.

Commenter Avatar March 25 / #

Took your advice but for some reason it doesn’t seem to be working – this is the code I use on the last line, but it stays with http://www.:

RewriteRule ^(.*)$ http://mollermarketing.com/$1 [L,R=301]

thanks for the help.

Commenter Avatar July 20 / #

Very useful tip. Thanks!

Commenter Avatar September 21 / #

@Leafnode stop exaggerate. RewriteEngine is the best way to do it.

Commenter Avatar January 08 / #
Mike says:

Heres an interesting one, I am in the process of moving a site from one host to another. My new server has apache userdir turned on so my site is visible on

exampleserver.com/~mysite
and
mysite.com

I have had a problem before with this sort of setup where google discovered both ways into the site and penalised me for duplicate content.

Unfortunately I cannot turn Userdir off.

Is there a way of preventing this sort of leak with htaccess?
Thanks for any help :D

Commenter Avatar February 16 / #
Ahmed says:

You can also use this:

RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]

RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Commenter Avatar August 22 / #
Sean K. says:

So does it matter if google site: only indexes the domain without www? Example, I have channelkelley.com. All the pages are listed without the www, but none are listed when you use http://www.channelkelley.com. I guess that’s fine if google penalizes for duped content. I just want to ensure I’m getting the most mileage from the engines. :)

Commenter Avatar August 28 / #
epsi says:

Thank you so much David.
Very useful to hide Joomla administrator App.

I’ve combined and put your tips here.
http://forum.joomla.org/viewtopic.php?f=432&t=418458

Commenter Avatar September 09 / #
Henry says:

Hi,

What then is the code if I have only one site on my server http://www.villa-stbarths.com but I want any-one typing just villa-stbarths.com to arrive on the index page at http://www.villa-stbarths.com/index.htm ?

Thanks, Henry

Commenter Avatar November 24 / #
Jow says:

Hey, I don’t think you need the “RewriteBase /” lin in there. It works fine for me without that line.

Commenter Avatar November 26 / #
Rukki Odds says:

Do not work for me :/ I’m tired of trying to dissappear www from my domain address.

However I have mod_rewrite enabled (I can still make my joomla have Use Apache mod_rewrite enabled)

Be Heard!

I want to hear what you have to say! Share your comments and questions below.

Name*:
Email*:
Website:  


© David Walsh 2007-2010. Contact David Walsh. Powered by the remarkable MooTools javascript framework.