Saturday, February 22, 2020

How to force redirect www or non-www using htaccess file?

Force www:

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

Force non-www:

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

Now, when you type in your domain name with either www in front or not, it should display as you have set it in the .htaccess file.

Be sure to replace 'yourwebsite.com' with your actual domain name.

Note: do not place both selections of code in the file as it will cause an error.

No comments:

Post a Comment