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} ^your-site.com [NC]
RewriteRule ^(.*)$ http://www.your-site.com/$1 [L,R=301]

Remove the www.

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


  1. Josh
    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?

    • NetHawk
      Permalink to comment#

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

    • Swastik Solutions
      Permalink to comment#

      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!

    • Go0se
      Permalink to comment#

      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.
      ServerName zanje.org

  3. Jayphen
    Permalink to comment#
  4. Sean
    Permalink to comment#

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

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

    • Swastik Solutions
      Permalink to comment#

      It is working on my website, getback with your file. Write here and would check it.

    • Morgester
      Permalink to comment#

      Was not a smart move to deploy something before testing it first.

  6. elandy2009
    Permalink to comment#

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

  7. black-light-studio
    Permalink to comment#

    #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]

  8. 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: http://www.site.com/rulezz -> site.com/
    instead of what I’d like: site.com/rulezz
    Any ideas?

  9. Said Bakr

    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!

  10. rarsa
    Permalink to comment#

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

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

  12. Christopher Burton

    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.

    • Christopher Burton


      Have you tried something like this?

      RewriteEngine On
      RewriteCond %{HTTP_HOST} !^www\. [NC]
      RewriteCond %{HTTP_HOST} (.*)
      RewriteRule  (.*) http://www.%1$1 [R=301,L]
    • Fred Gandt
      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 apache.org, 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.

    • Marc
      Permalink to comment#

      Hey Christopher,

      if I try yours, URLs of subpages get re-directed to the home page. Any idea why?

      So: https://mydmain.com/mypage/ goes back to https://mydmain.com/index.php



  13. Andy


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

    RewriteEngine on
    RewriteBase /

    RewriteCond %{HTTP_HOST} ^mysite\.
    RewriteRule .* http://www.mysite.com%{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 http://www.mysite.com/contact to http://www.mysite.com/#/contact 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 mysite.com/#/contact, rather than http://www.mysite.com/#/contact they just get http://www.mysite.com. If they try mysite.com/contact they will get http://www.mysite.com/#/contact.

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

  14. Martijn van der Ven

    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 http://www.www.extra-www.org 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]
  15. Joel
    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.


  16. Mat
    Permalink to comment#

    Thanks! Works fine.


  17. dany
    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, http://domain.com/category -> http://category.domain.com

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

  18. Andrew
    Permalink to comment#

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

  19. Parveen Kaushik
    Permalink to comment#


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


  20. Dean Lynn

    Works Perfectly! Thanks

  21. Parveen Kaushik

    I can remove the .html extension using following code

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

  22. 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: https://www.example.com 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.

  23. 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!

  24. rachid
    Permalink to comment#

    Great job, There are another tutorial about .htaccess, it provides some useful examples, also an example about forcing www http://www.bewebdeveloper.com/tutorial-about-advanced-htaccess-examples

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

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

  27. Ukeyd
    Permalink to comment#

    Thank you! : ))

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 triple backtick fences like this:

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

We have a pretty good* newsletter.