How To Force SSL By Editing .htaccess file On Your Website?

SSL securely transforms data from client to the server by adding encryptions. It was generally used for protecting transactions and customer data but now it is an official Internet ranking signal, as said by the Google.

To force your website use SSL, make it process links over HTTPS and not HTTP, you will simply need to add a few lines of code in .htaccess file of your website. The file is located in the root directory of your website.

By default .htaccess file is a hidden file so if in case you are unable to find it on your cPanel hosting’s file manager then click file manager’s ‘Settings’ icon and tick ‘Display hidden files’ option. If it is still not there then probably your website is not using any .htaccess file. Simply create one and open it for editing.

Forcing SSL Sitewide

In order to force HTTPS connection to all your web traffic use the following lines replacing URL.com with your own domain

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

Remember: If you see some existing code in your .htacess file then add these lines above where rules are starting with similar prefix.

Forcing SSL Over A Specific Domain

Condition-2: For forcing SSL over a specific domain:

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

Forcing SSL On A Sub Directory

Condition-3 For forcing SSL on some specific directory such as url.com/subdir.

First create a new .htaccess file in that specific directory and then use the following code:

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

Don’t forget to change URL.com with your own domain name and SUBDIR with the name of the sub directory where you want to force HTTPS connection.

If you don”t have an SSL Certificate, you can get one from here. If you have any questions, feel free to mention them below in the comment section.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.