Treehouse: Grow your CSS skills. Land your dream job.

Last updated on:


You should really be doing one or the other. For consistency, as well as SEO's, sake.

Force the www.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

Remove the www.

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


  1. Permalink to comment#

    Funnily enough, I was just looking for this… Thanks!

  2. Big W
    Permalink to comment#

    And why this site is not working without a “www”.

    you have to put www to load the page

    • feilips
      Permalink to comment#

      A and Aname?

    • Permalink to comment#

      It depends on the server configuration. Most server will work without www, but I come across others once in a while.

    • It is working on my website. Check for the configuration above once again.

    • Syed
      Permalink to comment#

      Well you seem to have some issues with your DNS records. Run a check and it should work!

    • You should add a ServerAlias to the apache file (assuming the site is server by apache) like to:

      DocumentRoot /path/to/website/files
      ServerAlias www.

  3. Sean
    Permalink to comment#

    Won’t this remove any subdomain? Not just the www. Just wondering.

  4. Permalink to comment#

    I just put this on my clients site and now its not working. They are a major manufacturer and this is freaking me out!!

  5. elandy2009
    Permalink to comment#

    Simply a thing: it requires that Apache has enabled the rewrite module. You can ask your host for some information.

  6. #nearly universal www – Resolve (takes away WWW from the URL on both HTTP and HTTPS)

    RewriteEngine on
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

    RewriteCond %{HTTPS} on
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

  7. Jayjay

    When I try this in my .htaccess, the path is being removed:

    RewriteEngine on
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*) http://%1/$1 [R=301,L]

    This is what I get: ->
    instead of what I’d like:
    Any ideas?

  8. Hi,
    Removing www from the url of the website by this way may leads to stop sub-domains. I’d like to know an alternative way that make .htaccess to do that for www only not any thing before the first dot!

  9. rarsa
    Permalink to comment#

    am a newbie here
    could u explain what special characters in .htaccess files mean… in the above example

  10. Permalink to comment#

    Tnx for code, just looking for it. It works in my case but with strange effect.

    I have two domains, one is parked. If I put non-www code for both domains I get “redirect loop”. If I put code for just one of them it works – but second one is redirected to first domain. Probably it is normal for parked domains but I did not know and was surprised,, and it was just what I wonted :)))

  11. I use the following on my site

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    • Andy

      Christopher Burton your code works perfect for me but how do I rewrite it to force www? This is doing my head in! Thanks.

    • @Andy

      Have you tried something like this?

      RewriteEngine On
      RewriteCond %{HTTP_HOST} !^www\. [NC]
      RewriteCond %{HTTP_HOST} (.*)
      RewriteRule  (.*) http://www.%1$1 [R=301,L]
    • Permalink to comment#

      Thanks Christopher :-)

      I was using the code at the top for a while with no bother, but just added a subdomain which it redirected.

      So far it appears that your code is only redirecting attempts at the www subdomain, and is letting through all others.

      Perfect! Long may it continue (browser (un)caching and/or general weirdness may kick in at any time after all)!

      Any tips for where best to find details about the .htaccess language?

      I can’t find anything very helpful at, but then I may be looking at the wrong pages.

      Any pointers (to save constantly having to look for pre-constructed one-time solutions (like yours above)) would be appreciated.

  12. Andy


    Got it to work in my crude way, see line 5&6 below:

    RewriteEngine on
    RewriteBase /

    RewriteCond %{HTTP_HOST} ^mysite\.
    RewriteRule .*{REQUEST_URI} [R=301,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* index.php

    The second part of the mod_rewrite is for the swfaddress system I use which changes to which allows my Flash site to be indexed by search engines etc.

    The problem is REQUEST_URI doesn’t recongnise the # symbol so if someone tries to access, rather than they just get If they try they will get

    This is the best I’m going to achieve I think but I do appreciate your help, thanks.

  13. To build upon black-light-studio’s and Christopher Burton’s solutions. This will remove any number of www. subdomains. I don’t expect this to ever really happen, but what with the initiative you never know what some crazy server administrator might do.

    RewriteEngine on
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^(www\.)+(.*)$ [NC]
    RewriteRule ^(.*)$ http://%2/$1 [R=301,L]
    RewriteCond %{HTTPS} on
    RewriteCond %{HTTP_HOST} ^(www\.)+(.*)$ [NC]
    RewriteRule ^(.*)$ https://%2/$1 [R=301,L]
  14. Permalink to comment#

    Apache recommends not using a RewriteRule for this sort of behavior. Rewrite processing can introduce more overhead to your website. You don’t want that rewrite rule to run over and over for valid URLs.

  15. Permalink to comment#

    Thanks! Works fine.


  16. Permalink to comment#

    this is work.. thanks alot..

    i want ask something about .htaccess, and i have problem for this..
    I have website and i want category be subdomain, like this, ->

    can somebody help me for my problem.. thank you…

  17. Permalink to comment#

    Simply a thing: it requires that Apache has enabled the rewrite module.

  18. Hi,

    I am working on static html website & now i want to hide “.html” extension Please give a any solution ASAP.


  19. Dean Lynn

    Works Perfectly! Thanks

  20. I can remove the .html extension using following code

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.*)$ $1.html

  21. Mathieu Jonson
    Permalink to comment#

    I’ve seen a million posts on how to add/remove the “www”. Likewise, I’ve seen a million posts on how to force http/https connections. How do you do both? I’m currently stuck: I can force https, and I can remove “www”, except when it’s combined. Example: stays the same.

    • Chris
      Permalink to comment#

      I am also looking for a solution to redirect http/https and remove the www. Would love to hear if anyone has a solution.

  22. Annette
    Permalink to comment#

    This will be a really easy question for this forum I am sure. I am not a web designer. Google originally indexed my parked domain (when on GoDaddy) as a non-www. It’s live now. And I submitted to google as www for site map and fetching. And it the site pages have been indexed and showing when I search with www. But when I search in Google for it as a non-www the parked domain information appears for my website. How can I reconcile/ merge/ remove the first non-www google had originally indexed with my live www website.

    Thank you!

  23. rachid
    Permalink to comment#

    Great job, There are another tutorial about .htaccess, it provides some useful examples, also an example about forcing www

  24. Ivan
    Permalink to comment#
  25. Richard
    Permalink to comment#

    Hey! Thanks this is awesome! I needed this for my companies website, you are the best!

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";