How do I redirect www to non-www or vice versa (http and https)? Print

  • 3152

To do this, simply create a .htaccess file in the public_html folder on your account (if it does not already exist), and add the following line to it (if it isn't already there)

RewriteEngine On

The next step is to add the redirect function to .htaccess. Depending on what you wish to do, below are some copy and paste examples that should work. Simply replace your_domain.com with the domain name you wish to use the code for.

To redirect non-www to www

RewriteCond %{HTTP_HOST} ^your_domain.com$
RewriteRule ^(.*)$ http://www.your_domain.com/$1 [R=301]

To redirect www to non-www

RewriteCond %{HTTP_HOST} ^www.your_domain.com$
RewriteRule ^(.*)$ http://your_domain.com/$1 [R=301]

To redirect non-www to www (while using SSL)

RewriteCond %{HTTP_HOST} ^your_domain.com$
RewriteCond %{SERVER_PORT} ^443
RewriteRule ^(.*)$ https://www.your_domain.com/$1 [R=301]

To redirect www to non-www (while using SSL)

RewriteCond %{HTTP_HOST} ^www.your_domain.com$
RewriteCond %{SERVER_PORT} ^443
RewriteRule ^(.*)$ https://your_domain.com/$1 [R=301]

The last 2 are meant for online stores using SSL or to prevent any SSL errors while using SSL on some pages.


Was this answer helpful?

« Back