Non-secure redirects
non-www to www
Google Chrome recently started hiding the www portion of URLs, but if you want your website to work via https://www.yourdomain.com instead of https://yourdomain.com, you can set this up using a 301 redirect.
Learn how to use 301 redirects in your .htaccess file in our quick guide.
You will need to add following code to the beginning of your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule ^(.*)$ https://yourdomain/$1 [L,R=301,NC]
Make sure to replace yourdomain.com with your actual domain name.
Once you save the file, your website will be automatically redirected from https://yourdomain.com to https://www.yourdomain.com.
www to non-www
To redirect your website from https://www.yourdomain.com to https://yourdomain.com, put in the following code to the beginning of your .htaccess file:
RewriteEngine on RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC] RewriteRule ^(.*)$ https://yourdomain.com/$1 [L,R=301,NC]
Make sure to replace yourdomain.com with your actual domain name.
Secure redirects
Redirect entire domain to HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Redirect entire domain from HTTP to HTTPS and non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
For all of the code snippets, make sure to always place the code at the beginning of your .htaccess file in the public_html directory of your hosting control panel.