In this tutorial we’ll perform redirect of HTTP to HTTPS (mod_rewrite – Apache) htaccess rule on CWP, I’m assuming you’ve already installed ssl certs and enabled https support for your website if you not done it yet then stop here this can break websites if ssl is not already installed.
Using Apache as main server (only) :
Forcing non www to www and https :
Forcing www to non www and https :
this also add Permanent 301 Redirect.
Using Apache as backend server behind NGINX/VARNISH server :
This need to be added in your sites .htaccess :
Rewrite rule as follows, this need to be added in your sites .htaccess :
OR
OR
To force all traffic to use both the www domain and SSL HTTPS, use the following rules:
To force all traffic to use non www domain and SSL HTTPS, use the following rules:
OR
replace “domain\.tld” with domain name and tld eg “nulledinfo\.com”
Using Apache as main server (only) :
Code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Code:
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]
Code:
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]
Using Apache as backend server behind NGINX/VARNISH server :
This need to be added in your sites .htaccess :
Rewrite rule as follows, this need to be added in your sites .htaccess :
Code:
SetEnvIf X-Forwarded-Proto "https" HTTPS=on
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Code:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
Code:
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.tld$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.tld$
SetEnvIf X-Forwarded-Proto "https" HTTPS=on
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteRule ^ https://domain.tld/$1 [L,R=301]
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.tld$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.tld$
SetEnvIf X-Forwarded-Proto "https" HTTPS=on
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]