Redirect all requests to www.website (or non-www)

Redirecting your website from non-www to www URLs can be as simple as adding suitable code in your .htaccess file.

For non-WordPress websites, you can use the code below replacing domainname.com with your own domain name.

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

For WordPress websites, this should be done by setting the URLs in your WordPress General Settings. Using the code above for a WordPress website can put it in a loop.

You can also force non-www URLs in a similar way by changing the code as follows. Again, this should be placed in your .htaccess file located in your public_html folder.

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

Did you find this article useful?