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]



Redirect Www Urls To Non-Www With Https


RewriteCond %{HTTPS} on 


RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] 


RewriteRule ^(.*)$ https://%1/$1 [R=301,L]   


To force non-www urls www with https 


RewriteCond %{HTTPS} on


RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]


RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]      



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