Redirect www to non-www with .htaccess

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!

2 Responses to “Redirect www to non-www with .htaccess”

  1. 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]

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

Leave a Reply