The Best Way to Redirect for WWW and Non-WWW with .htaccess

To avoid confusing search engines, it's important to have a properly configured 301 redirect for your base domain. You must decide whether to use the 'www' sub-domain or to ditch the antiquated prefix in favor of a cleaner URL.

301 redirects can have various use-cases. I’ll cover using a 301 redirect to prevent search engines from indexing the wrong URLs. For instance, we don’t want both ‘www.yoursite.com’ and ‘yoursite.com’ indexed with the same information, or what is called duplicate content.

Note: This how-to is based on the Apache web server method. You’ll need to consult your hosting provider support to implement these changes if you’re website isn’t on an Apache server.

Why is this the best method for implementing 301 redirects? We aren’t making assumptions on where the files are being served from. The beauty of this method is that we can use this snippet of code on any domain. I like to work locally and really don’t want to configure a seperate .htaccess file for my local machine and another one for my dev and production servers. This also plays well with SubVersion or Git repositories and remote teams.

First, you’ll need to determine whether your URLs will have the ‘www’ prefix or not. It’s important to chose a structure and stick with it as you don’t want to be changing it in the future.

You’ll then want to locate your .htaccess file. If you don’t have an .htaccess file, crack open your favorite text editor and create a new black file in the root of your websites public folder (usually ‘public_html’ or ‘httpdocs’). And be sure to save the file as ‘.htaccess’, not ‘htaccess.txt’.