A Web Design Community curated by Chris Coyier

A little dab'll do ya

Code Snippets

Home » Code Snippets » HTAccess » WWW / No-WWW Submit one!

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]

Subscribe to The Thread

  1. Josh says:

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

  2. Big W says:

    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/

  3. Sean says:

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

  4. Jack says:

    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 says:

    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 says:

    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. Said Bakr says:

    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!

It's Your Turn

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
--- The Management ---