Blog

Force www or non-www in htaccess for WordPress

Many customers at some point request to force either the www or non-www version of their site to display in their visitor’s browser. For example, you can have www.example.com or simply example.com display in an address bar.

Force www

Look for the .htaccess via FTP and copy/paste this code. Be sure to replace ‘example.com‘ with your actual domain name.

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

Force non-www

Look for the .htaccess via FTP and copy/paste this code. Be sure to replace ‘example.com‘ with your actual domain name.

#Force non-www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.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.