How to remove www from your URL with mod_rewrite

I got a hit today for the following search query: how do you get rid of the www in url. As you can see that hits on my article about removing PHPSESSID's, which isn't quite what the person was looking for I guess. Here's the code to 301 redirect the www version of your site to the non-www version using Apache's mod_rewrite:

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

Adding the www instead of removing it

And, as requested in the comments, the code to add www to your domain name:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com$1 [R=301]

38 Responses to “How to remove www from your URL with mod_rewrite”

  1. I've used a version without the domain name for my blog, see this post.

  2. Great way to serve your vistors and search engines by keeping eye on what you are being found for then serving up something trully useful.

  3. Can i invert the procedure? i mean i want to auto add the www not to remove it.

    Can I do it?

  4. Hi, Joost!

    I want to auto add the www not to remove it.

    I've added this to htaccess:

    RewriteCond %{HTTP_HOST} ^example.com$
    RewriteRule (.*) http://www.example.com$1 [R=301,L]

    BUT some images just disappeared.

    What should I do?

    Thank you in advance

  5. I was wondering how you would delete parts within a url. For example:
    http://www.example.com/folder1/folder2/page.html

    would be rewritten so the user would see

    http://www.example.com/page.html

    Any help is much appreciated, considering working with mod_rewrite is a real pain!

  6. Very interesting site. Thanks you
    .

  7. I'll throw in another vote for Arjan's fix. I can use the same .htaccess on a bunch of sites that way.

  8. Should not the rewrite code have backslashes before the dots ("example\.com")?

  9. Hey,

    I'm trying to remove to redirect all /index.php to just /

    Any ideas?

  10. I'm trying to achieve the removal of www from my URL's but the directive above has no effect. This is probably because I dont use .htaccess but enter the directives into the ServerConfig. Any idea if this could be the cause and what needs to be changed to get it working?

  11. Thanx for the quick response. I checked and all my other mod_rewrite rules seem to do their job just fine so I guess it is enabled.

    Check this for instance: http://poundingtechno.com/counter/

    This rule was created to redirect all hits to the main domain:

    # PREVENT HOTLINKING ON COUNTER DOWNLOADS
    Options +FollowSymlinks
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^/counter
    RewriteRule ^.*$ - [L]
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?poundingtechno.com(/)?.*$ [NC]
    RewriteRule .*\.(php)$ http://poundingtechno.com [R,NC]

  12. I know, but what about the appreciation of the page behind the 301 redirect?

    It's recommend to use the correct domain during the linkbuilding.

  13. Ok I am new to WP and after setting up my site gmctopkicks.com I dont have the www at all? if you type in http://www.gmctopkicks.com it goes there but then takes out the www?

    Any ideas what I did wrong and how to fix it? Or does it even matter?

    Thanks in Advance.
    Mike

  14. I dont even know where the code goes. Dont even know how I did it. Wow you answer fast! Thanks!
    Mike

  15. Thanks. So is it better for Google to have it or not have it? Why do people remove it?

  16. Thanks for the great code, however, it doesn't filter down to subdirectories.

    As an example, when I place the code in my .htaccess file in my public_html folder, it works great for the domain:

    http://www.domain.com --> domain.com

    However:

    http://www.domain.com/city/ --> http://www.domain.com/city

    (It has no effect unless I'm in the root folder.

    Any help?

    Thanks

  17. Thanks for the great code, however, it doesn't filter down to subdirectories.

    As an example, when I place the code in my .htaccess file in my public_html folder, it works great for the domain:

    http://www.domain.com --> domain.com

    However:

    http://www.domain.com/city/ --> http://www.domain.com/city

    (It has no effect unless I'm in the root folder.

    Any help?

    ---------------
    the answer is

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www.domain.com/city/$[NC]
    RewriteRule ^(.*)$ http://domain.com/city/$1 [R=301,L]

  18. sorry this the answer for B question
    ---
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www.example.net/city/$ [NC]
    RewriteRule ^(.*)$ http://example.net/city/$1 [R=301,L]

  19. hello bro,i have got my website at http://www.soamjena.com and i want to remove the ending SLASH / which automatically comes when someone opens my site in their browser. Kindly help me and get me a good solution.
    Im waiting for your kind reply.

  20. hi
    i want to rewrite http://www.someting.com to something.com,but it hav to be done automatically for all sites am going to host in my dynamically configured apache...
    so can u pls giv a soltion to dis problm....
    thanks in advance

  21. How can I resolve my home page URL to always display http://www.example.com/ even when someone clicks on http://www.example.com/index.html or if someone types http://example.com/ also it should resolve to http://www.example.com/

    Thanks in advance!

  22. Sorry for my earlier post...but below is my actual problem.
    my site works on https://example.com/index.php but https://www.example.com/index.php gives me certificate error.
    i want to redirect anything from https://www.example.com to https://example.com
    Can anyone plz help me.

  23. Thanks for the code, Joost. Very useful and works like a charm

  24. So I spent all morning trying to figure out how to do this from the Apache manual, and was on the point of giving up when I googled "mod_rewrite remove www.", got your post, and it was working in minutes! Thanks.

  25. hello, thank you for your help,
    i try to do the job with url, and exclude index.php on my htaccess with your code:
    RewriteBase / RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
    RewriteRule ^index\.php$ http://www.mywebsite.com/ [R=301,L]

    the problem is that i have an internal error 500...

  26. How i can cange the code for a multistore view (with mutiple domains)?

Comments closed, if you feel you have something to say:
drop me a line.

4 Trackbacks to “How to remove www from your URL with mod_rewrite”

  1. 爱喝白开水的空间博客 - Fri, July 24th, 2009 at 01:36