Apache HTTP to HTTPS htaccess redirect on CWP – Centos WebPanel

by Sandeep B.

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.

SSL is very important nowadays for better security and SEO benefits, if you didn’t installed ssl for your sites yet consider upgrading to https, this will build trust towards your visitors and google gives better ranking in search engine (I’m not a SEO expert but noticed this changes).

How to Enable HTTP/2 on CWP7 – Centos Web Panel

Lets get started :

Using Apache as main server (only) :

Rewrite rule as follows, this need to be added in your sites .htaccess :

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

Forcing non www to www and https :

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]

Forcing www to non www and https :

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]

this also add Permanent 301 Redirect.

If upper rules doesn’t works for your Websites And Getting “Too Many redirects” then you should consider below htaccess rule.

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 :

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]

OR

RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

OR

RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

To force all traffic to use both the www domain and SSL HTTPS, use the following rules:

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]

To force all traffic to use non www domain and SSL HTTPS, use the following rules:

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]

OR

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]

replace “domain\.tld” with domain name and tld eg “mysterydata\.com”

Hope this Post was helpful, stay tuned will be more in future.

If this post helps you in any way please consider a donation

Donate with PayPal :

md-donate

You may also like

12 comments

Hasan Fakih July 31, 2018 - 11:23 am

Using Apache as backend server behind NGINX/VARNISH server and using the below config only working for http://www.domain.com but not working for domain.com:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Reply
Hasan Fakih July 31, 2018 - 11:29 am

I used the below code and it’s working sucessfully:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L,NE]

Reply
Sandy July 31, 2018 - 2:40 pm

Thanks for sharing

Reply
Loeng Khuoch August 11, 2018 - 4:18 pm

Dear,
I am Using Apache as backend server behind NGINX/VARNISH server in CWP.
I use this method:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://admin.example.com%{REQUEST_URI} [R=301,L,NE]

but it does not work. The url forced to https://admin.example.com but the it shows error content.
In firefox with private window with clear cache and cookie:
The page isn’t redirecting properly
An error occurred during a connection to admin.example.com.
This problem can sometimes be caused by disabling or refusing to accept cookies.

Any one help me, please.

Thanks,

Reply
Sandy August 11, 2018 - 4:42 pm

HELLO
if you’ve any redirects under script side (not in .htaccess) then you need to disable it.

Reply
Mohit Singla September 17, 2018 - 5:07 pm

Thanks This Code Really Works.
Tried
https://hostiko.in/
https://www.hostiko.in/
http://hostiko.in/
http://www.hostiko.in/
All are going to https://hostiko.in/

RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Reply
Nga Dang December 17, 2019 - 8:16 am

You asked yourself, I want to change http to https of webmail in CWP, how about it?
I tried following your .htaccess but failed.
i use Apache.
Thank you so much.

Reply
Sandy December 17, 2019 - 8:56 am

this topic is for https redirect of website not the webmail. Kindly create a topic in forum

Reply
Nga Dang December 17, 2019 - 9:29 am

OK, thank you.

Reply
Andy January 9, 2020 - 7:02 am

Hi,
Can I ask, where is the folder that contains the .htaccess file located on CWP?
I see the Wiki for CWP is just located at /usr/local/apache/htdocs/.htaccess
But I tried your methods still not working.

Reply
Sandy January 9, 2020 - 8:37 am

hi you need to create .htaccess in the root dir of your script.

Reply
Andy January 10, 2020 - 1:47 am

Hi,
Is CWP root directory located at:
/ usr / local / apache / htdocs /
Right, did you create the file there?
If I said it in the right place, I would have tried it and still couldn’t.

Reply

Leave a Comment