Redirecting www to non-www
Welcome back! Good to see you. If you haven't seen it, here's the archive of my best writing. Thanks for visiting!
I microblog now more than I blog at Twitter.com/stuntdubl
Feel free to drop me a line sometime or subscribe in case I ever post again.
I had several examples of 301 redirecting the other way around, from non-www to www, but hadn’t ever had to redirect from www to non-www for anything.
In case your wondering, and don’t ever want the headache I have from my eyes crossing from mod-rewrite, here is how to do it:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule ^.*$ http://example.com%{REQUEST_URI} [R=301,L]
If you have more rewrite questions, you can try the mod rewrite cheat sheet, or the URL re-writing tools, or the guide to 301 redirects.
Don’t redirect to example.com, and be sure to check your header responses when you are through.
Thanks to Louis, and a gent who does teak furniture for their assistance in this fiasco.











3 Comments
graywolf
October 24th, 2005,
6:31 pm
I do it without the “www” for “branding” or heck just because it sound “nicer”. The REQUEST URI is a new one for me, going to have to try that out.
Sebastian
October 25th, 2005,
4:57 am
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example\.com [NC]
RewriteRule (.*) http://example.com/1 [R=301,L]
should do the trick too, and it covers case issues in the server name.
Installing Wordpress and Setting Permalinks
December 16th, 2009,
5:50 pm
[...] In Dreamhost you can set this when you’re setting up the site. For other installs, check out Stuntdubl’s guide or use a [...]