Apr
24
2009

Redirecting www to non-www Using .htaccess

By Gabriel Harper Posted in

Most websites have both a www and non-www version of the domain name, but typically they both show the same thing. If you’re concerned about how search engines are viewing your website, or just want to maintain a consistent URL for all users, you can automatically redirect visitors to the non-www version of your domain name (and vice versa).

The Apache Web server supports URL rewriting with the mod_rewrite engine. Creating custom rewrite rules in your .htaccess file gives you the ability to use custom URLs, redirect URLs and handle requests differently based on the URL format. One of the best tricks with mod_rewrite and .htaccess is redirecting traffic from the www to non-www version of your domain. Of course, you can also use .htaccess to redirect from the non-www to www URL as well.

Which is better, www or non-www?

The choice between using www or non-www domain name for your URLs is really just a matter of preference. Whichever one you choose is less important than being consistent with the choice. Displaying the same content for the www and non-www versions of your site can result in duplicate content. Since there isn’t really a right or wrong choice, the important thing is just to make sure all of your visitors get redirected to the same place. Using just two lines in .htaccess, you can define rules on the server that automatically redirect non-www to www, or redirect www to non-www.

The .htaccess Rules

You might already have an .htaccess file on your server, in which case you can edit your existing .htaccess rules. If not, create a new .htaccess file, including the period at the beginning. Choose the appropriate rule for redirecting www to non-www, or non-www to www, and add the code to your .htaccess file. Replace example.com with your website’s domain name.

Redirect www to non-www:

Copy the following .htaccess code to redirect users from www to non-www:

1
2
3
4
RewriteEngine On
RewriteBase /
RewriteCond % ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

Redirect non-www to www:

Copy the following .htaccess code to redirect users from non-www to www:

1
2
3
4
RewriteEngine On
RewriteBase /
RewriteCond % ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

The .htaccess rules shown above will send a search engine friendly 301: Redirect HTTP header. Search engines will then know it is a permanent move and will crawl the new URL in the future. These rules also preserve the full URL (so example.com/about will redirect to www.example.com/about). Search engines can are good at figuring out which format is used more often, you’re already showing the same page for www and non-www URLs. By forcing users to redirect to one or the other, you can be consistent about your URL formats and make sure you aren’t showing any duplicate content or confusing the search engines any more than necessary.

About The Author

Gabriel Harper

Gabe is the owner and founder of Intavant, and contributes to Intavant Blog regularly with his expertise in design, development & business.

Did you find this article helpful? Please subscribe!