WWW / No-WWW

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]

Comments

  1. User Avatar
    Josh
    Permalink to comment#

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

  2. User Avatar
    Big W
    Permalink to comment#

    And why this site is not working without a “www”.
    http://znanje.org/
    you have to put www to load the page
    http://www.znanje.org/

    • User Avatar
      feilips
      Permalink to comment#

      A and Aname?

    • User Avatar
      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.

    • User Avatar
      Swastik Solutions
      Permalink to comment#

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

    • User Avatar
      Syed
      Permalink to comment#

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

    • User Avatar
      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. User Avatar
    Jayphen
    Permalink to comment#
  4. User Avatar
    Sean
    Permalink to comment#

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

  5. User Avatar
    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!!

    • User Avatar
      Swastik Solutions
      Permalink to comment#

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

    • User Avatar
      Morgester
      Permalink to comment#

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

  6. User Avatar
    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. User Avatar
    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. User Avatar
    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. User Avatar
    Said Bakr

    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!

  10. User Avatar
    rarsa
    Permalink to comment#

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

  11. User Avatar
    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. User Avatar
    Christopher Burton

    I use the following on my site

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    • User Avatar
      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.

    • User Avatar
      Christopher Burton

      @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]
    • User Avatar
      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.

    • User Avatar
      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

      Cheers,

      /marc

  13. User Avatar
    Andy

    @Christopher

    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. User Avatar
    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. User Avatar
    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.

    http://httpd.apache.org/docs/2.4/rewrite/remapping.html#canonicalhost

  16. User Avatar
    Mat
    Permalink to comment#

    Thanks! Works fine.

    Cheers
    Mat

  17. User Avatar
    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. User Avatar
    Andrew
    Permalink to comment#

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

  19. User Avatar
    Parveen Kaushik
    Permalink to comment#

    Hi,

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

    Thanks

  20. User Avatar
    Dean Lynn

    Works Perfectly! Thanks

  21. User Avatar
    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. User Avatar
    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.

    • User Avatar
      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. User Avatar
    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. User Avatar
    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. User Avatar
    Ivan
    Permalink to comment#
  26. User Avatar
    Richard
    Permalink to comment#

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

  27. User Avatar
    Ukeyd
    Permalink to comment#

    Thank you! : ))

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag