I had a heck of a time finding out how to do this. Seems like everyone wants to know how to redirect the non-www version of their site to the www version. So, for those of us who want to redirect the www version of our websites to the non-www version, put this in your .htaccess file in your root folder:
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule (.*) http://%1/$1 [R=301,L]
For those of you that want to do the opposite and redirect the non-www version of your site to the www version of your site, use the following instead:
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.domain.net/$1 [R=301,L]
Hope this helps!
{ 3 comments… read them below or add one }
You looked everywhere but forgot to ask your dad. Here is a transportable unit, requires no change from site to site:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ http://%{SERVER_NAME}/$1 [R=301,L]
hey – it cut off part of the code:
between options and rewrite is the ifModule mod_rewrite.c and at the end is the end IfModule (/).
Thanks,
I’ve been using your transportable code, and it worked smoothly.
Now, I don’t have to type specific domain name for each of my sites.