How to Force www or non-www in htaccess

October 26, 2018

If you want to redirect non-www requests to the www URL of your website (i.e. redirect from http://example.com to

http://www.example.com/) then you can do this with a few lines in your .htaccess file

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Don't forget to enable the rewrite engine!

Don't forget that you will probably need to enable the rewrite engine for these rules to work. Click the button below to find out how to do this.