How to remove www from your URL with mod_rewrite

April 08th, 2007 – 42 Comments

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]

42 Responses to How to remove www from your URL with mod_rewrite

  1. siuck
    By siuck on 24 November, 2009

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

  2. Jack001
    By Jack001 on 21 September, 2009

    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…

  3. kalpesh
    By kalpesh on 20 September, 2009
  4. John
    By John on 13 September, 2009

    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.

  5. Andries
    By Andries on 15 June, 2009

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

  6. vinod
    By vinod on 19 September, 2008

    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.

  7. Fred
    By Fred on 17 September, 2008

    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!

  8. vini
    By vini on 1 August, 2008

    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

  9. SoamJena
    By SoamJena on 10 April, 2008

    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.

  10. itti
    By itti on 29 March, 2008

    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]

  11. itti
    By itti on 29 March, 2008

    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]

  12. B
    By B on 21 February, 2008

    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

  13. Joost de Valk
    By Joost de Valk on 13 January, 2008

    It doesn’t matter which one you choose, as long as you choose either one.

  14. GMC TopKick
    By GMC TopKick on 13 January, 2008

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

  15. Joost de Valk
    By Joost de Valk on 13 January, 2008

    They go into your blog’s .htaccess

  16. GMC TopKick
    By GMC TopKick on 13 January, 2008

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

  17. Joost de Valk
    By Joost de Valk on 13 January, 2008

    Ehm Mike, that’s what this does. If you want it the other way around, use the second code.

  18. GMC TopKick
    By GMC TopKick on 13 January, 2008

    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

  19. Joost de Valk
    By Joost de Valk on 19 October, 2007

    @Henri: if you can, that’s best of course, but sometimes your clients changes it’s brand when you’re halfway, and then you’ll thank god on your bare knees for the 301 redirect ;)

  20. Henri
    By Henri on 19 October, 2007

    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.

  21. Glitch
    By Glitch on 18 September, 2007

    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]

  22. Joost de Valk
    By Joost de Valk on 18 September, 2007

    Ehm no this should work, you should check whether your mod_rewrite is enabled at all…

  23. Glitch
    By Glitch on 18 September, 2007

    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?

  24. Joost de Valk
    By Joost de Valk on 7 September, 2007

    Hey Dan, you could try to catch that inside the PHP file…

  25. Dan
    By Dan on 7 September, 2007

    Hey,

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

    Any ideas?

  26. ryan
    By ryan on 25 August, 2007

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

  27. Zapper Canada
    By Zapper Canada on 15 July, 2007

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

  28. Jesse
    By Jesse on 10 May, 2007

    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!

  29. Joost de Valk
    By Joost de Valk on 3 May, 2007

    Hmm Sergey, perhaps you’ve got some code in your site preventing hotlinking which now breaks?

  30. Joost de Valk
    By Joost de Valk on 23 April, 2007

    Updated the post as well.

  31. Joost de Valk
    By Joost de Valk on 23 April, 2007

    Yeah sure:

    RewriteCond %{HTTP_HOST} ^example.com$
    RewriteRule (.*) http://www.example.com$1 [R=301,L]
  32. Daniel Sanchez
    By Daniel Sanchez on 22 April, 2007

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

    Can I do it?

  33. Joost de Valk
    By Joost de Valk on 10 April, 2007

    Thanks Aaron, I hope it pays off :)

  34. Aaron Pratt
    By Aaron Pratt on 9 April, 2007

    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.

  35. Joost de Valk
    By Joost de Valk on 9 April, 2007

    Yeah I see, sucks when PageRank splitting happens, so you’d better be sure to fix it up front when you can :)

  36. Arjan Eising
    By Arjan Eising on 9 April, 2007

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


Check out our must read articles about Analytics