How to Redirect HTTP to HTTPS, [Force HTTPS htaccess]

How to Redirect HTTP to HTTPS, Learn about SSL and how to force the secure version of the hypertext transfer protocol, a.k.a., HTTPS.

Updated: 01 Jan, 23 by Antoniy Yushkevych 4 Min

List of content you will read in this article:

If a website doesn't have an SSL certificate, then Firefox and Google Chrome shows "insecure warnings" when visitors load that website. SSL encrypted connection is necessary for the accessibility and safety of visitors to your website. 

It is also essential to redirect HTTP to HTTPS. Still, there is a way for forcing a website to load it into HTTPS (Hypertext Transfer Protocol Secure) rather than HTTP(Hypertext Transfer Protocol); by editing the .htaccess file. So this particular article will give detailed information on "how to force HTTPS" with step by step procedure.

How to Redirect HTTP to HTTPS

Secure Sockets Layer is the full form of SSL, so it works for the security protocol for creating encrypted links between browsers and a web server for online communication. Your website will be available on HTTPS and HTTP after installing an SSL certificate in it. SSL makes sure that data transmitted between browsers and a web server always stays encrypted.  

Once you have the SSL certificate, install it on your website hosted by a VPS hosting. Hence, VPS hosting plays an essential role in creating a link between the SSL certificate and your website. 

We will cover a detailed procedure to edit the .htaccess file and force HTTPS with steps for easy understanding (Ensure to create a backup of the website to eliminate major troubles).  So there are different ways of editing the .htaccess file for your website, and these ways are: 

  • You can edit the .htaccess file on a desktop/laptop then upload it back on the server by FTP(File Transfer Protocol).
  • You can use the cPanel's file manager to edit a file.
  • You can use FTP's Edit mode, which offers a way to edit a file easily or use an SSH(Secure Shell) and text editor for editing. 
  • You can use the cPanel file manager to edit the .htaccess file. 

If you have multiple domains, but a single domain requires force, use the code below. (We are writing a domain name as "domainxyz.com" in the code so make sure you change it while copying for your domain name).

RewriteEngine On

RewriteCond %{HTTP_HOST} ^yourdomain1.com [NC]

RewriteCond %{HTTPS} off

RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

After copying these codes, click on the save to make changes successfully. 

You can use a function as the 301 redirects for .htaccess that can permanently redirect an old URL to the new URL. So use the following steps for performing it:

First, go to the File Manager of your hosting panel, open the public_html folder, and then select the .htaccess file.

Now, scroll down and find RewriteEngine On, then write the below codes in it: 

RewriteEngine On

RewriteCond %{HTTPS} off

RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Finally, save the changes to change HTTP to HTTPS quickly (Ensure that don’t repeat a line in RewriteEngine On because it can create significant trouble for your website).

You can use a .htaccess file to force HTTPS on a particular folder but make sure to place this file into a folder having an HTTPS connection. So use the following command to force HTTPS on the particular folder: 

RewriteEngine On

RewriteCond %{HTTPS} off

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

Ensure that you change a folder reference to the name of the actual directory. Once you make the changes, clear the cache of your web browser and connect to your website through HTTPS, and it will redirect you to HTTPS. 

This wraps up our article on “how you can force HTTPS” quickly, and it can be a useful option if you want to change your website from HTTP to HTTPS. We have included the guidelines for multiple scenarios, so this article can help you to force HTTPS for specific conditions. Use this procedure if you face any issues while installing an SSL certificate on your website while using VPS hosting, then let us know in the comments.

Antoniy Yushkevych

Antoniy Yushkevych

Master of word when it comes to technology, internet and privacy. I'm also your usual guy that always aims for the best result and takes a skateboard to work. If you need me, you will find me at the office's Counter-Strike championships on Fridays or at a.yushkevych@monovm.com
user monovm

ToTemat

2021, Nov, 21

And what is better to choose, redirect through the code in htaccess or how to force in the server settings. Which method will be better?