O'Reilly

No WWW Using .htaccess

By on  

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]

Save some URL characters and remove WWW from your site!

O'Reilly Velocity Conference
Save 20% with discount code AFF20

Recent Features

  • How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

  • Write Simple, Elegant and Maintainable Media Queries with Sass

    I spent a few months experimenting with different approaches for writing simple, elegant and maintainable media queries with Sass. Each solution had something that I really liked, but I couldn't find one that covered everything I needed to do, so I ventured into creating my...

Incredible Demos

  • Implement jQuery’s hover() Method in MooTools

    jQuery offers a quick event shortcut method called hover that accepts two functions that represent mouseover and mouseout actions. Here's how to implement that for MooTools Elements. The MooTools JavaScript We implement hover() which accepts to functions; one will be called on mouseenter and the other...

  • jQuery Comment Preview

    I released a MooTools comment preview script yesterday and got numerous requests for a jQuery version. Ask and you shall receive! I'll use the exact same CSS and HTML as yesterday. The XHTML The CSS The jQuery JavaScript On the keypress and blur events, we validate and...

Discussion

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

  2. @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.

  3. 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

  4. @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.

  5. 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.

  6. 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.

  7. Very useful tip. Thanks!

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

  9. Mike

    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

  10. Ahmed

    You can also use this:

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

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

  11. Sean K.

    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. :)

  12. 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

  13. 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

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

  15. Rukki Odds

    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)

  16. Thanks David – this is the shortest and most concise explanation for running a site without WWW. It’s working like a charm on one of our new sites!

  17. I try it but it’s not work. When I try into www. but it don’t redirect to non-www

  18. How do handle https pages as well?

  19. How do you handle https pages as well?

  20. Thanks a lot , really usefull , and resolve the ^(.+)\/$ config

    Really thanks

  21. Although it might be sexy, do not use this when you want cookieless subdomains like a CDN (content delivery network) or to avoid cookies on other content of your page. Every subdomain and all content of your domain receives these cookies when landing on yourdomain.com, stylesheets, images, everything. This can be a performance gain like ,for example, this document is explaining – http://www.ravelrumba.com/blog/static-cookieless-domain/

    The landing page must http://www.yourdomain.com to avoid cookies on, for example, img.yourdomain.com.

    Google has also documentation about this, it is a part of Google Speedtest. That’s a very technical story but very interesting to read to avoid unnecessary overhead on your website. I followed this Speed Guide and have implement it at a couple of websites and results are amazing. Caching is also a good thing, safes more bandwidth, server doesn’t need the power before, sites are speedier even on shared hosting and especially on handheld devices.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!

Recently on David Walsh Blog

  • Prevent Chrome from Translating a Page

    A while back I shared my favorite Google Chrome extension:  Google Art Project.  I've enjoyed seeing beautiful art when I open a new tab -- it's brought genuine happiness to my day, however small that happiness may be.  About a week ago, however, the art presented had...

  • Create Any Type Of Website With These Multi-Purpose Themes

    We have selected what we believe are the very best multipurpose WordPress themes on the market today. Our list contains a number of best sellers, several newcomers that are proving to be highly popular, and a few themes that are ideal for creating the types of...

  • An Introduction to Static Site Generators

    Static site generators seem to have been becoming more and more popular recently, but they’re not one of those ephemeral novelty things that grow in popularity as quickly as they fall into oblivion shortly after. For over a decade, many different projects — 394 of...

  • Automated Tests for Visual Responsive Layouts

    Today it's all about testing. In 2015, many developers knows about TDD and I personally think that testing is one of the key for quality products. But what about testing in a Front-end environment? How do you guys write your tests for a responsive page or...

  • Getting Dicey With Flexbox

    What if you could build complex CSS layouts in minutes? Flexbox is a new CSS layout spec that makes it easy to construct dynamic layouts. With flexbox, vertical centering, same-height columns, reordering, and direction agnosticism are a piece of cake. There's a popular myth floating around that...