WordPress 301 Redirect Non-WWW to WWW
In order to avoid a potential duplicate content penalty with Google, some webmasters choose to redirect accesses to the non-www version of their domain to the www version (or vice versa).
If you are running WordPress, the following will explain one way of redirecting all access attempts made to your site without the www to URLs at your website that include the www portion of the domain. The result is a 301 Redirect.
In your .htaccess file in the web root folder, add the following after the Rewrite On line:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Change both instances of example.com to your domain name. Be sure to escape (\) the period(s) in the RewriteCond statement (example\.com).
Update: The above works, but only until you create a new page…then WordPress kindly writes over it – Bleah. To get around this, I do the following (note: I am NOT saying it’s the correct and proper way to do it…just that it works for me and I haven’t found any other way yet). Basically, here’s what I’ve done. I’ve placed the following ABOVE the # BEGIN WordPress code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
</IfModule>
That way WordPress can edit the stuff inbetween # BEGIN WordPress and # END WordPress and leave this stuff alone!
—
Caution: Mod_rewrite is an extremely powerful tool. Whenever using mod_rewrite, test thoroughly and use with care. No warranty of code is expressed or provided. Use at your own risk.