Knowledge Base Hub

Browse through our helpful how-to guides to get the fastest solutions to your technical issues.

Home  >  SSL Issues & FAQ  >  Steps to Force SSL Using the .htaccess
Top Scroll

Steps to Force SSL Using the .htaccess

 2 min

SSL certificate plays a significant role on your website. It helps to build the trust of your visitors as well as encrypts the data that is shared by your customers on your website. The data such as a customer’s personal details, credit/debit card information, bank details, etc. is secured with SSL. Additionally, having the SSL certificate installed on your website also helps in boosting your website’s ranking in the search engines.

So, the visitors on your website should be accessing it using an SSL-encrypted connection for additional security, accessibility or PCI compliance.

MilesWeb offers SSL certificate with its entire web hosting plans. You can activate this SSL in your Account Management Panel (AMP) under My Account > Manage Free SSL.

You can force visitors to use SSL through your .htaccess file using mod_rewrite.

Forcing HTTPS with .htaccess

1. Redirect All Web Traffic

If you want to force all web traffic to use SSL certificate, run the following code in the .htaccess file in your website’s root folder.

NOTE: If there is an existing code in your .htaccess, add this code above where you will find rules with a similar starting prefix. If there are any issues afterwards, move the new code below some elements – e.g. Options +FollowSymLinks.

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]

2. Redirect Only Specified Domain

In order to force a specific domain for using HTTPS, you can use the below code in the .htaccess file in your website’s root folder:

NOTE: If there is an existing code in your .htaccess, add this code above where you will find rules with a similar starting prefix. If there are any issues afterwards, move the new code below some elements – e.g. Options +FollowSymLinks.

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 the above code isn’t working, remove the first two lines and run the code as below:

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

Ensure that you replace example.com with the domain name for which you are forcing https. Also, you need to replace www.example.com with your actual domain name.

3. Redirect Specified Folder

In case you want to force SSL on a specific folder, input the below code into a .htaccess file that is placed in that particular folder:

NOTE: If there is an existing code in your .htaccess, add this code above where you will find rules with a similar starting prefix. If there are any issues afterwards, move the new code below some elements – e.g. Options +FollowSymLinks.

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]

Ensure that you change the folder reference to the original folder name. Then make sure to replace www.example.com/folderwith your original domain name and folder, you want to force the SSL on.

In this way, you can easily force SSL on any website using the .htaccess.

For our Knowledge Base visitors only
Get 10% OFF on Hosting
Special Offer!
30
MINS
59
SECS
Claim the discount before it’s too late. Use the coupon code:
STORYSAVER
Note: Copy the coupon code and apply it on checkout.