When creating any site, it is best practice to make sure that all pages on a site either render only for the www version (i.e. www.domain.com) or the non-www version (domain.com). In order to do this, you need to set up a 301 redirect in .htaccess for one version or the other.
WordPress is really good at this out of the box, but sometimes things go wonky (like in the case of our multisite redirection fiasco) and you need to reset the redirect in .htaccess. Strangely, these snippets are always painful for me to find on the web. So here they are, for my future reference and yours.
301 Redirect from nonwww to www
Ex. from wpgarage.com to www.wpgarage.com
Also ensures pages redirect to the exact new page address: Ex. wpgarage.com/about to www.wpgarage.com/about
1 2 3 |
<code title="in your .htaccess file">ewriteEngine On RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]</code> |
301 redirect from www to nonwww
Ex. from www.wpgarage.com to wpgarage.com
Also ensures pages redirct to the exact new page address: Ex. www.wpgarage.com/about to wpgarage.com/about
1 2 3 |
<code title="in your .htaccess file">RewriteEngine On RewriteCond %{HTTP_HOST} !^my-domain\.com$ [NC] RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L]</code> |
I believe it is the (.*) in the 3rd line that turns it into a wildcard redirect, rather than just redirecting to the homepage.