SOLVED HTTPS Redirection For All VirtualHosts

THawke

Member
Jun 16, 2017
6
1
3
Canada
cPanel Access Level
Root Administrator
Looking for a way to redirect http traffic to https across all VirtualHosts by default. To try to accomplish this I've copied vhost.default to vhost.local in /var/cpanel/tempaltes/apache2_4 and have added various methods for redirection to the bottom of the vhost template including:
Code:
Redirect / https://[% wildcard_safe(vhost.servername) %]/
and
Code:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
I have tried a few different Rewrite rules to try to get it to work. However, whatever I use seems to redirect to an https url okay, but is creating a redirect loop. It appears to be generating the correct configuration in httpd.conf according to the documentation I've read, so I'm not sure where the redirect loop is coming from. It's a freshly spun up VM, everything is default other than a few imported cpanel accounts for testing and the vhost redirection snippet I'm trying to inject via the template.
 

THawke

Member
Jun 16, 2017
6
1
3
Canada
cPanel Access Level
Root Administrator
Forgot to mention that I'm using the LetsEncrypt autossl provider, I'm not sure that would affect anything. Using apache and cpanel config defaults and going to an accounts cpanel and adding a redirect from / to https://domain redirecting with and without www. and with or without wild card redirect is also creating the redirect loop.
 

THawke

Member
Jun 16, 2017
6
1
3
Canada
cPanel Access Level
Root Administrator
Putting
Code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTPHOST}%{REQUESTURI}
into a file at the specified path is working as expected. Thanks.
 

THawke

Member
Jun 16, 2017
6
1
3
Canada
cPanel Access Level
Root Administrator
Alright, never mind, it's still not functioning properly. Without the configuration I can request a test domain with a "Hello World" index page with CURL from both http and https. When I add this configuration
Code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTPHOST}%{REQUESTURI}
in /etc/apache2/conf.d/userdata/std/2_4/redirect.conf, I can request the file via curl from https directly okay, but when I request the http, I get curl: (47) Maximum (50) redirects followed. Fresh vps and cpanel, only added a test account. OS CENTOS 6.9 x86_64 cPanel & WHM 64.0
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,904
2,226
463
Hello,

Could you open a support ticket using the link in my signature so we can take a closer look? You can post the ticket number here so we can update this thread with the outcome.

Thank you.
 

THawke

Member
Jun 16, 2017
6
1
3
Canada
cPanel Access Level
Root Administrator
Putting:
Code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
In a .conf file under /etc/apache2/conf.d/userdata works well. There was also an issue with CloudFlare proxy that was not requesting via http so https requests would come back with the redirect to http causing the loop.
 
  • Like
Reactions: cPanelMichael