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]




by Arjan Eising on 9 April, 2007 at 18:27
I've used a version without the domain name for my blog, see this post.
by Joost de Valk on 9 April, 2007 at 20:03
Yeah I see, sucks when PageRank splitting happens, so you'd better be sure to fix it up front when you can :)
by Aaron Pratt on 9 April, 2007 at 23:07
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.
by Joost de Valk on 10 April, 2007 at 08:03
Thanks Aaron, I hope it pays off :)
by Daniel Sanchez on 22 April, 2007 at 23:23
Can i invert the procedure? i mean i want to auto add the www not to remove it.
Can I do it?
by Joost de Valk on 23 April, 2007 at 07:57
Yeah sure:
RewriteCond %{HTTP_HOST} ^example.com$ RewriteRule (.*) http://www.example.com$1 [R=301,L]by Joost de Valk on 23 April, 2007 at 08:00
Updated the post as well.
by Sergey on 2 May, 2007 at 22:27
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
by Joost de Valk on 3 May, 2007 at 09:34
Hmm Sergey, perhaps you've got some code in your site preventing hotlinking which now breaks?
by Jesse on 10 May, 2007 at 03:27
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!
by Michael Heath on 7 July, 2007 at 06:22
Very interesting site. Thanks you
.
by Zapper Canada on 15 July, 2007 at 21:52
I'll throw in another vote for Arjan's fix. I can use the same .htaccess on a bunch of sites that way.
by ryan on 25 August, 2007 at 13:09
Should not the rewrite code have backslashes before the dots ("example\.com")?
by Dan on 7 September, 2007 at 13:50
Hey,
I'm trying to remove to redirect all /index.php to just /
Any ideas?
by Joost de Valk on 7 September, 2007 at 14:50
Hey Dan, you could try to catch that inside the PHP file...
by Glitch on 18 September, 2007 at 18:04
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?
by Joost de Valk on 18 September, 2007 at 18:14
Ehm no this should work, you should check whether your mod_rewrite is enabled at all...
by Glitch on 18 September, 2007 at 18:21
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]
by Henri on 19 October, 2007 at 07:43
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.
by Joost de Valk on 19 October, 2007 at 08:32
@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 ;)
Pingback: Paradigm.ru » Blog Archive » «Ð”иктую: даблъюдаблъюдаблъю… ЗапиÑ?али?..»
by GMC TopKick on 13 January, 2008 at 21:49
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
by Joost de Valk on 13 January, 2008 at 21:51
Ehm Mike, that's what this does. If you want it the other way around, use the second code.
by GMC TopKick on 13 January, 2008 at 21:56
I dont even know where the code goes. Dont even know how I did it. Wow you answer fast! Thanks!
Mike
by Joost de Valk on 13 January, 2008 at 21:58
They go into your blog's .htaccess
by GMC TopKick on 13 January, 2008 at 22:03
Thanks. So is it better for Google to have it or not have it? Why do people remove it?
by Joost de Valk on 13 January, 2008 at 22:05
It doesn't matter which one you choose, as long as you choose either one.
Pingback: IDIOMS development » Blog Archive » Front end research links
by B on 21 February, 2008 at 00:21
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
by itti on 29 March, 2008 at 00:44
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]
by itti on 29 March, 2008 at 00:47
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]
by SoamJena on 10 April, 2008 at 14:56
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.
by vini on 1 August, 2008 at 18:29
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
by Fred on 17 September, 2008 at 13:12
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!
by vinod on 19 September, 2008 at 11:05
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.
Pingback: Magento Stores Ecommerce Search Engine Optimization, Magento store, magento shop SEO | Magento - Shopping Cart
by Andries on 15 June, 2009 at 19:39
Thanks for the code, Joost. Very useful and works like a charm
Pingback: 爱喝白开水的空间博客
by John on 13 September, 2009 at 17:06
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.
by kalpesh on 20 September, 2009 at 07:20
how can i covert url like this
http://www.site.com/City/Index.php?City=Newyork
to
http://Newyork.site.com/City/Index.php
by Jack001 on 21 September, 2009 at 22:37
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...
by siuck on 24 November, 2009 at 11:17
How i can cange the code for a multistore view (with mutiple domains)?