force https for a given website and not all hosted accounts

trucmuche

Well-Known Member
Nov 20, 2014
93
4
58
cPanel Access Level
Root Administrator
Hello,

I host many websites using WHM/CPanel and for ONE of them, I would like to force my visitors to use HTTPS instead of HTTP. Of course, I have already installed a SSL certificate and I can perfectly see my website using https://subdomain.mydomain.com.

Is there a way to force HTTPS on ONE account and not globally ?
Is there a WHM/CPanel solution or should I use .htaccess ??

Thank you very much for your help !

T.
 

Eminds

Well-Known Member
Nov 10, 2016
319
32
28
India
cPanel Access Level
Root Administrator
To force all web traffic to use HTTPS insert the following lines of code in the .htaccess file in your website’s root folder.

Important:If you have existing code in your .htacess, add this above where there are already rules with a similar starting prefix.


Code:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Be sure to replace www.example.com with your actual domain name.

To force a specific domain to use HTTPS, use the following lines of code in the .htaccess file in your website's root folder:
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Make sure to replace example\.com with the domain name you're trying force to https. Additionally, you need to replace example.com with your actual domain name.
 
Last edited by a moderator:

trucmuche

Well-Known Member
Nov 20, 2014
93
4
58
cPanel Access Level
Root Administrator
Thanks ! So, to ensure that I understand correctly your answer (because I'm not sure to understand perfectly the difference between the two alternatives you're talking about - what's the difference ?)...

I would like to redirect the HTTP visitors of my subdomain to HTTPS address (when they try to access my website using HTTP, switch them to HTTPS). So I have to put a .htaccess file in the /home/mydomain/subdomain.example.com/ folder and write inside :
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://subdomain.example.com/$1 [R,L]
Is that right ? Or should I put this .htaccess (also ?) in /home/mydomain/public_html directory ?

Thanks again !
 
Last edited by a moderator:

SysSachin

Well-Known Member
Aug 23, 2015
604
48
28
India
cPanel Access Level
Root Administrator
Twitter
You will have to use following code in your /home/mydomain/public_html directory and same code in your subdomain directory (/home/mydomain/subdomain.mydomain.com/), Just change your domain name to sub-domain.

Code:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
 

cPanelMichael

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

I'm happy to see the provided information was helpful. Let us know if you have any additional questions.

Thank you.