How to redirect HTTP to HTTPS Using .htaccess? Linux

Browsers such as Chrome and Firefox have started showing security warnings on sites with valid SSL certificates. Without SSL, the website shows up as insecure to the visitors. Hence, it becomes very important to redirect HTTP requests to HTTPS.

SSL – Secure Sockets Layer (SSL) is a standard security protocol that is used for establishing encrypted links between the browser and the webserver. SSL certificate is required to create an SSL connection. Once you provide Company information and activate SSL on your web server, two cryptographic keys are created – Private and Public Key

To force your web traffic to use HTTPS, the following codes needs to be edited in the .htaccess file

Step 1: Backup the current .htaccess file before editing.

Step 2. Login to cPanel and go to Files -> File Manager -> Document Root

Step 3: Select the domain name you want to edit

Step 4: Enable Show Hidden Files and click on Go

This will open a new window with the files and folder list

Step 6: Right-click on the .htaccess file and click on edit and continue

Step 7: Save the changes once the edit is done

Step 8: Test your website for the changes

HTTP request to HTTPS – Redirect all web traffic

Edit the existing .htaccess code by adding the following lines

RewriteEngine On

RewriteCond %{SERVER_PORT} 80

RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]

HTTP request to HTTPS – Redirect only a specific domain web traffic

Edit the existing .htaccess code by adding the following lines

RewriteEngine On

RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC]

RewriteCond %{SERVER_PORT} 80

RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]

HTTP request to HTTPS – Redirect only a specific folder

Edit the existing .htaccess code by adding the following lines

RewriteEngine On

RewriteCond %{SERVER_PORT} 80

RewriteCond %{REQUEST_URI} folder

RewriteRule ^(.*)$ https://www.yourdomain.com/folder/$1 [R,L]

At Velan, our server support engineers can help you secure your websites by redirecting your HTTP traffic to HTTPS

. We troubleshoot problems like these for our clients every day. If you are interested in our service, please fill the Quick connect form to get in touch with us

Credentials

Quick Connect With Us