Force Http To Https using .htaccess (2021)

Your website is now accessible via HTTP and HTTPS after you install an SSL certificate. However, it is preferable to use only HTTPS because it encrypts and secures the data on your website. Although 7Hosty allows users to customize this setting with a single click, you can also force HTTPS connections using the  .htaccess file. We’ll show you how in this article.

 to https redirect htaccess

Force HTTPS on ALL Traffic Using Htaccess :

301 redirects are one of the many functions you can perform with .htaccess, which permanently redirects an old URL to a new one. Follow these steps to enable the function that forces HTTPS on all incoming traffic:

  1. In your cPanel, go to File Manager and open .htaccess in the public html folder. If you can’t find it, Be sure to Create It or unhide it.
  2. Scroll down to RewriteEngine On and add the following code lines below it:
    RewriteEngine On 
    RewriteCond %{HTTPS} off 
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  3. Save the changes.
Note: Make sure that the line RewriteEngine On is not repeated twice. In case the line already exists, simply copy the rest of the code without it.

Force HTTPS on Particular Domain Using Htaccess:

Consider the following scenario: you have two domains: http://domain1.com and http://domain2.com. While both domains access the same website, only the first should be redirected to the HTTPS edition. In this case, the following code must be used:

 

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^domain1.com [NC] 
RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Make sure to replace domain1 with the actual domain you’re trying to force HTTPS on.

Force HTTPS On Particular Folder:

You may also use the .htaccess file to force HTTPS on individual folders. The file should, however, be saved in the folder that will be used for the HTTPS link.

 

RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteRule ^(folder1|folder2|folder3) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Make sure the folder references are changed to the correct directory names.

Clear your browser’s cache and try connecting to your site via HTTP after you’ve made the changes. The browser would guide you to the HTTPS version if everything was entered correctly.

Conclusion

Congratulations! You have successfully updated your .htaccess file to redirect all HTTP traffic to HTTPS, your website’s secure edition. There may be other ways to allow this function depending on the platform on which you built your website. Plugins, for example, can be used to customize your WordPress or PrestaShop site to work with HTTPS.

Also Checkout : How To Login Into cPanel Account

 

%d bloggers like this: