Htaccess force HTTPS

Htaccess force HTTPS – a Complete Guide

Once an SSL certificate has been installed, both HTTP and HTTPS are supported by your website. The latter, however, is preferable because it encrypts and safeguards the data on your website. To force an HTTPS connection, you can utilize the .htaccess file in addition to Hostinger’s one-click configuration option. This guide will demonstrate how.

Htaccess force HTTPS - a Complete Guide

What differences are there between HTTP and HTTPS?

With encryption and authentication, HTTPS equals HTTP. The sole distinction between the two protocols is that HTTPS employs TLS (SSL) to encrypt and digitally sign requests and answers made using regular HTTP. Because of this, HTTPS is much safer than HTTP. An HTTP website’s URL begins with http://, while an HTTPS website’s URL begins with https://.

What is HTTP?


HTTP, which stands for Hypertext Transfer Protocol, is a protocol—or a set of rules for the presentation of information—that is used to move data over a network. The HTTP protocol is used to transmit the majority of data transferred over the Internet, including API calls and website content. Requests and replies are the two primary categories of HTTP messages.

What is HTTPS?

What differences are there between HTTP and HTTPS?


HTTPS means HTTP + S which stands for “secure” in HTTPS. An attacker would see a collection of seemingly random characters in the example above because HTTPS employs TLS (or SSL) to encrypt HTTP requests and responses.

How does HTTPS assist with web server authentication?


Verifying a person or machine is who they say they are is known as authentication. HTTP is founded on the idea of trust rather than identity verification. The creators of HTTP just had concerns other than security at the time and didn’t necessarily decide to implicitly trust all web servers. But authentication is crucial on the current Internet.

A private key certifies server identity in the same way as an ID card does for an individual. Possession of the private key that matches the public key in an SSL certificate for a website demonstrates that the server is indeed the authorized host of the website when a client creates a channel with an origin server (for example, when a user navigates to a website). Several attacks that could occur in the absence of authentication are prevented or helped to block by this, including:

  • On-the-go assaults
  • DNS spoofing
  • hijacking of BGP
  • Spoofing a domain

The certificate authority that issued the SSL certificate also digitally signs it. This demonstrates that the server is indeed who it seems to be.

http vs https

Force HTTPS on All Traffic

One of the many tasks you can accomplish with .htaccess is a 301 redirect, which permanently switches a URL from one location to another. The following steps will enable the feature to force HTTPS on all incoming traffic:

  1. Open the .htaccess file located in the public_html folder by going to File Manager in your hosting control panel. Make sure to create or unhide it if you can’t find it.
  2. Add the following lines of code beneath RewriteEngine On by scrolling down and finding it:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  • 3- Save changes.

Force HTTPS on a Specific Domain

Assume you own the domains http://domain1.com and http://domain2.com. You only want the first domain to be forwarded to the HTTPS version because they both reach the same page. You should use the following code in this situation:

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

Ensure that domain1 is changed to the domain that you are attempting to impose HTTPS on.

Force HTTPS on a Specific Folder

Additionally, the.htaccess file can be used to force HTTPS on particular folders. The file should be put in the folder with the HTTPS connection, though.


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

Ensure that the folder references are updated to reflect the correct directory names..

After making the modifications, try connecting to your site using HTTP while clearing the cache in your browser. You will be forwarded to the HTTPS version by the browser if everything was added properly.

Force HTTPS using WordPress Plugins

WP Force SSL enables you to automatically resolve SSL problems and reroute unsafe HTTP traffic to secure HTTPS. All will be set and SSL enabled once Force SSL is activated. Your SSL certificate will be used to convert the entire website to HTTPS. Any SSL certificate will do.

How can I enable and add SSL?

Log into your hosting panel and install an SSL certificate because the majority of hosting providers provide Let’s Encrypt’s free SSL certificate. You will notice a button that says “Add SSL Certificate” or “Add Let’s Encrypt Certificate,” and with just one click, WP Force SSL will enable SSL on your website. If it doesn’t work, you can create a free SSL certificate for your website using WP Force SSL PRO. and every 90 days will regenerate the SSL certificate.

Should I force HTTPS (SSL)?

When a website uses HTTPS instead of HTTP, all connections between your browser and that website are secured using an SSL (Secure Socket Layer). Making sure your website launches securely over HTTPS is a good practice even if it doesn’t deal with sensitive data.

Final Words

Congratulations! Your .htaccess file has been successfully modified to send all HTTP traffic to HTTPS, the secure version of your website. There can be other ways to enable this functionality depending on the platform on which you built your website. For instance, you can use plugins to set up your WordPress or PrestaShop website to function with HTTPS.

We would love to hear any advice, suggestions, or tricks you have to provide in the comments section!

WP Q and A
Eng.M.Thatwart is a developer and author at wpqanda, who is passionate about WordPress, digital marketing, and website builders. He enjoys sharing his knowledge through his writings.