Force www or non-www with htaccess

Want to make sure all visitors only see one version of your website that uses the www prefix?

Add this to your htaccess file.

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

Or remove the www for your domain by adding the following.

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

– LAST TESTED DECEMBER 9, 2019

Code not working? Let us know in the comments below.

Latest Snippets

Leave a comment