Grow your CSS skills. Land your dream job.

Last updated on:

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

    http://znanje.org/

    you have to put www to load the page

    http://www.znanje.org/

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

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

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

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

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

  15. Permalink to comment#

    Thanks! Works fine.

    Cheers
    Mat

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

    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.

    Thanks

  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: https://www.example.com stays the same.

  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 http://www.bewebdeveloper.com/tutorial-about-advanced-htaccess-examples

Leave a Comment

Posting Code

  • Use Markdown, and it will escape the code for you, like `<div class="cool">`.
  • Use triple-backticks for blocks of code.
    ``` 
    <div>
      <h1>multi-line block of code</h1>
      <span>be cool yo.</span>
    </div>
    ```
  • Otherwise, escape your code, like <code>&lt;div class="cool"&gt;</code>. Markdown is just easier though.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".