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]





I've used a version without the domain name for my blog, see this post.
Yeah I see, sucks when PageRank splitting happens, so you'd better be sure to fix it up front when you can :)
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.
Thanks Aaron, I hope it pays off :)
Can i invert the procedure? i mean i want to auto add the www not to remove it.
Can I do it?
Yeah sure:
RewriteCond %{HTTP_HOST} ^example.com$ RewriteRule (.*) http://www.example.com$1 [R=301,L]Updated the post as well.
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
Hmm Sergey, perhaps you've got some code in your site preventing hotlinking which now breaks?
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!
Very interesting site. Thanks you
.
I'll throw in another vote for Arjan's fix. I can use the same .htaccess on a bunch of sites that way.
Should not the rewrite code have backslashes before the dots ("example\.com")?
Hey,
I'm trying to remove to redirect all /index.php to just /
Any ideas?
Hey Dan, you could try to catch that inside the PHP file...
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?
Ehm no this should work, you should check whether your mod_rewrite is enabled at all...
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]
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.
@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 ;)
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
Ehm Mike, that's what this does. If you want it the other way around, use the second code.
I dont even know where the code goes. Dont even know how I did it. Wow you answer fast! Thanks!
Mike
They go into your blog's .htaccess
Thanks. So is it better for Google to have it or not have it? Why do people remove it?
It doesn't matter which one you choose, as long as you choose either one.
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
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]
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]
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.
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
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!
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.
Thanks for the code, Joost. Very useful and works like a charm
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.
how can i covert url like this
http://www.site.com/City/Index.php?City=Newyork
to
http://Newyork.site.com/City/Index.php
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...
How i can cange the code for a multistore view (with mutiple domains)?