Redirect www to non-www with .htaccess

by admin on March 7, 2009

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 }

Dean Isaacson March 7, 2009 at 4:37 pm

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]

Dean Isaacson March 7, 2009 at 4:39 pm

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 (/).

Coal Centre November 7, 2010 at 4:00 pm

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.

Leave a Comment

Previous post:

Next post: