mattgadient.com

301 Redirect from WWW to non-WWW (and back) through .htaccess

I had surprising difficulty finding information about changing a site from www.website.com to just website.com through redirects. I also wanted to be sure that it would redirect any directories and pages as well. Since I found some information here (a great page if you’re looking to do a copy-paste to your own .htaccess file), I thought I’d post what I’d found:

First, to redirect from www.website.com to website.com…… (tested by me and works):

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^website.com
RewriteRule (.*) http://website.com/$1 [R=301,L]

Just copy and paste that into your .htaccess file, probably towards the top. Don’t forget to change “website” to the name of your site.
If on the other hand, you would like to change website.com to www.website.com, try this (tested by me and works):

RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www.website.com [NC]
RewriteRule (.*) http://www.website.com/$1 [R=301,L]

or try this (haven’t tested it but I’d imagine it would work):

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^website.com [NC]
RewriteRule ^(.*)$ http://www.website.com/$1 [L,R=301]

Again, don’t forget to change “website” to the name of your own site.

I tried the examples on a couple of web sites (and am now using them). If someone tries to go to a directory or page on the “old” url, they will be directed to the new page automatically. So if someone tried to go to www.website.com/relatives/aunts/over50/matilda.html, it would transfer the visitor to website.com/relatives/aunts/over50/matilda.html. And going non-www to www works the same way.

Possible problems:

I came across one situation where these redirects would cause a problem… subdomains, wildcarded subdomains or a wildcarded DNS. If you don’t know what a wildcarded subdomain is, and if you don’t use subdomains, chances are you don’t have to worry about it. I made a slight change to cure the problem though that works when redirecting from non-www to www:

RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} ^website.com [NC]
RewriteRule (.*) http://www.website.com/$1 [R=301,L]

If you have subdomains, this should keep them from being redirected to your main site.

Why Redirect?

The main reason to redirect is because of search engines. Many search engines see www.website.com and website.com as two entirely different sites. Now normally that’s not a big deal, except the search engines will see that they have the exact same content, and might believe that one is simply a copy of the other. The search engine will often penalize one of the sites. Therefore, it’s often a good idea to redirect, just to be on the safe site.

0 Comments

 | Leave a Comment

    Leave a Comment

    All fields are optional - you can leave name/email/website blank if you prefer to show up as "Anonymous". Gravatars are supported. You can check the privacy policy for more details.