Knowledge Base » Blog Archive » Redirect all requests to www.website (or non-www)

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

August 4th, 2016

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.

# ensure www.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]]

Alternate code:

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]