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.

1. 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

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]

RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]

</IfModule>

2. 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

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteCond %{HTTPS} off

RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

</IfModule>

3. 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

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteCond %{HTTPS} off [OR]

RewriteCond %{HTTP_HOST} !^www\. [NC]

RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]

RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]

</IfModule> 

4. 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:

# 301 REDIRECT HTTP TO HTTPS AND WWW TO NON-WWW

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteCond %{HTTPS} off [OR]

RewriteCond %{HTTP_HOST} ^www\. [NC]

RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]

RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

</IfModule>

Was this answer helpful? 0 Users Found This Useful (0 Votes)