Table of Contents
With .htaccess, you can perform 301 redirects, i.e., permanently redirecting an old URL to a new one.
How to Redirect HTTP to HTTPS using .htaccess?
Follow these steps to force HTTPS on all incoming traffic:
Step 1: Log in to cPanel, and Under the FILES section, click on File Manager.
Step 2: A File manager window appears where you can see the public_html folder. You can navigate from either side, left or right.
Step 3: Browse to .htaccess inside the public_html folder. Be sure to create or unhide it if you can’t find it.
Step 4: Search RewriteEngine On text in the file and then insert the below codes.
RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] Step 5: Click Save the changes.
Forcing HTTPS on a Specific Domain
You can do that when you have multiple websites but want to get traffic in only one domain. For example, You have two domains: http://firstdomain.com and http://seconddomain.com accessing the same website. And you just wish for the first domain to be redirected to the HTTPS version.
Using the following codes to force HTTPS on a specific domain:
RewriteEngine On RewriteCond %{HTTP_HOST} ^firstdomain.com [NC] RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Forcing HTTPS on a Specific Folder
You can also use the .htaccess file to force HTTPS on particular folders. Place the file, however, in the folder that will have the HTTPS connection.
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(folder1|folder2|folder3) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Note: Do replace the folder name with the one you want to.
Clear the cache of your browser and try it again. You can now try to connect to your site via HTTP. Once all the required fields are filled, the browser will redirect you to the HTTPS version.
Therefore, from the above three ways, you can force traffic to your HTTPS Version.