Redirect http to https in WHM

Edubec

Registered
Feb 28, 2015
4
1
3
cPanel Access Level
Root Administrator
Hi :)

I have a VPS with several websites, and in one of them, I want to redirect from http to https.

Based on the Apache documentation I can do with this:
Code:
<VirtualHost *:80>
ServerName www.example.com
Redirect / https://www.example.com/
</VirtualHost >
I understand that the best way to do this in the WHM is:
Home> Service Configuration> Apache Configuration> Include Editor

And here is where I have a couple of questions:

1) Should I put the above code in: Pre Virtual Host include o Post Virtual Host include?

2) If in the future I would like to edit or delete the added code: Can I do it in the WHM? Or should I edit / delete the file included via SSH?

Thanks for the help friends and apologize if my English is faulty.

Eduardo
 
Last edited by a moderator:

cPanelMichael

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

You can actually add the following entry to the .htaccess file within the account's home directory:

Code:
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^$ https://www.example.com/%{REQUEST_URI} [R,L]
This is easier for the end-user to modify should they decide to change this behavior in the future.

Thank you.
 

neilt17

Registered
Jun 7, 2016
1
0
1
UK
cPanel Access Level
Website Owner
Shouldn't that last line be:
Code:
RewriteRule .* https://www.example.com/%{REQUEST_URI} [R,L]
Rather than:
Code:
RewriteRule ^$ https://www.example.com/%{REQUEST_URI} [R,L]
which will only redirect the home page but no other pages at www.example.com.
 
Last edited by a moderator:

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,904
2,237
463
Yes, there are several valid alternatives that redirect the pages differently than the example provided.

Thank you.