How to redirect HTTP to HTTPS Using htaccess

How to redirect HTTP to HTTPS Using htaccess

Insecure warnings are now visible in Chrome and Firefox on websites lacking SSL certificates. Visitors will see your website as insecure if SSL is not used. Hence, employing an SSL-encrypted connection is required for reasons of security, accessibility, or PCI compliance. Redirecting from HTTP to HTTPS becomes crucial.

To establish an SSL connection, you need an SSL certificate. As soon as you decide to enable SSL on your web server, you will need to provide all information on the identity of your website and your business. A Private Key and a Public Key are then generated as two cryptographic keys.

SSL (Secure Sockets Layer) is a widely used security technique for creating secure connections in online communications between web servers and browsers. All information passed between the web server and browser is kept secured thanks to the use of SSL technology.

Edit the codes in the.htaccess file to force HTTPS for all of your online traffic.

Here's how to change the.htaccess file before we proceed to redirect HTTP to HTTPS. Skip to the steps for redirection if you are familiar.

Switching from HTTP to HTTPS


1. All Online Traffic Should Be Redirected

Add the following code if your.htaccess file already contains it: 

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


2. Just reroute to a particular domain

Add the following to redirect a specific domain to HTTPS:

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


3. Only Redirect a Selected Folder

Add the following when redirecting to HTTPS on a certain folder:

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


Note: Wherever necessary, replace "yourdomain" with the name of your actual domain. Replace /folder with the real folder name in the case of the folder as well.

Hope this article will help you to redirect HTTP to HTTPS Using .htaccess