Forcing HTTPS with the htaccess File

Forcing HTTPS with .htaccess

NOTE: For WordPress sites, consider a SSL plugin such as Really Simple SSL instead.

Redirect All Web Traffic

To force all web traffic to use HTTPS, insert the following lines of code in the .htaccess file in your website’s root folder.

RewriteEngine On 
RewriteCond %{HTTPS} !on 
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$ 
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$ 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Redirect Only Specified Domain

To force a specific domain to use HTTPS, use the following lines of code in the .htaccess file in your website’s root folder:

RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteEngine On 
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

If this doesn’t work, try removing the first two lines.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

Make sure to replace example.com with the domain name you’re trying force to https. Additionally, you need to replace www.example.com with your actual domain name.

Redirect Specified Folder

If you want to force SSL on a specific folder, insert the code below into a .htaccess file placed in that specific folder:

RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} folder
RewriteRule ^(.*)$ https://www.example.com/folder/$1 [R=301,L]

Make sure you change the folder reference to the actual folder name. Then be sure to replace www.example.com/folder with your actual domain name and folder you want to force the SSL on.

 

Was this answer helpful?

Related Articles

How to Retrieve a CSR from cPanel

Have you generated a Certificate Signing Request (CSR) from cPanel but lost it? Then, you can...

How to Remove a CSR code in cPanel

You can remove the CSR code, which is no longer needed. Please, keep a backup of the CSR code for...

How to Install an SSL on Your Domain Using AutoSSL in cPanel

cPanel can automatically install an SSL on your domain within a few hours. However, even after 24...

How to Include or Exclude a Domain from AutoSSL in cPanel

You can include or exclude your domain from AutoSSL.If your domain is newly-created, wait a few...

How to Generate a Certificate Signing Request - CSR in cPanel

Obtaining a certificate from a trusted SSL provider requires a Certificate Signing Request (CSR)....