How to force https in your domain using htaccess ?

SSL is essential for protecting your website, even if it doesn’t handle sensitive information like credit cards. It provides privacy, critical security and data integrity for both your websites and your users’ personal information.

The primary reason why SSL is used is to keep sensitive information sent across the Internet encrypted so that only the intended recipient can understand it. This is important because the information you send on the Internet is passed from computer to computer to get to the destination server. Any computer in between you and the server can see your credit card numbers, usernames and passwords, and other sensitive information if it is not encrypted with an SSL certificate.

To know how to use SSL click here: How to enable external SSL in cpanel?

How to force https with .htaccess?

To force all the traffic to use HTTPS insert the following lines of code in the .htaccess file in your website’s root by editing it. >> public_html folder.

If you cannot see .htaccess file in your public_html folder, then click on settings button and check the box in front of show hidden files and save it.

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

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

If you want a specific domainĀ  to be forced to use HTTPS, use the following lines of code in the .htaccess file in your website’s root >> public_html folder:

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

If you want to force SSL on any specific folder use following code in the .htaccess file inside that specific folder:

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

Important: Don’t forget to replace the example.com with your real domain.

Add a Comment

Your email address will not be published. Required fields are marked *