Redirect HTTP & non-www to HTTPS & www URLs
Note: do not change ANYTHING in your .htaccess file without having a working copy of the file on your desktop.
Any typo in the file WILL bring your site down.
- Redirect non-www to www URL
Add the following code in the root folder .htaccess file to redirect the non-www to www URL.
Option 1:
RewriteEngine On RewriteCond %{HTTP_HOST} !^www. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Option 2:
301 REDIRECT NON-WWW TO WWW
- Redirect HTTP to HTTPS URL
Add the following code in the root folder .htaccess file to redirect the HTTP to HTTPS URL.
Option 1:
RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
Option 2:
301 REDIRECT HTTP TO HTTPS
- Redirect HTTP & non-www to HTTPS & www URLs
Add the following code in the root folder .htaccess file to redirect the HTTP to HTTPS URL. The following HTACCESS code snippet redirect all HTTP & non-www requests to HTTPS & www URL.
Option 1:
RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^(.)$ https://%{HTTP_HOST}/$1 [L,R=301] RewriteCond %{HTTP_HOST} !^www. RewriteRule ^(.)$ https://www.%{HTTP_HOST}/$1 [R=301,L] Option 2:
301 REDIRECT HTTP TO HTTPS AND NON-WWW TO WWW
- Redirect www & HTTP to non-www & HTTPS URL
Add the following code in the root folder .htaccess file to redirect the HTTP to HTTPS URL.
Option 1:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Option 2: