This post will show you how to redirect your non-www URLs to www, or vice-versa, using htaccess. Let's get started.


You have the choice - it doesn't really matter if you choose to redirect non-www to www or vice versa. It's a stylistic choice that comes down to whichever you prefer.


Redirect non-www to www

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



Redirect www to non-www

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


If you had a "RewriteEngine On" in your htaccess file already, insert the above code just after the line reading: "RewriteEngine On".