Knowledge Base

Redirect HTTP to HTTPS using the .htaccess

In this tutorial, I will demonstrate how you can redirect your website from HTTP to HTTPS using the .htaccess file.

In the past, you didn’t have to worry much about this topic unless you had a commercial website to sell services or products.

More recently, Chrome and Firefox have started showing insecure warnings on websites without SSL certificates – which may drive your visitors away.

In other words, it become important to use HTTPS on your website.

One of the options to force your visitors to use HTTPS on your website is to edit the .htaccess file – located at the root of your website public folder.

Prerequisites

Make sure you have the following prerequisites before starting to make changes to redirect the visitors of your website from http to https:

  • Your web server must be compatible with Apache (Litespeed also works).
  • Your website is already configured to use HTTPS and has an SSL Certificate already installed.
  • You should know how to locate the public directory of your website.
  • You should know how to view, edit and create a .htaccess file (if necessary) using whatever tools available.

Edit the .htaccess file

To redirect the visitors of your website from HTTP to HTTPS, begin by adding the following code to your .htaccess file – located at the public directory of your website:

# Turn RewriteEngine ON
RewriteEngine On
# Redirect http to https
RewriteCond %{HTTPS} !^on$
RewriteRule (.*) https://example.com/$1 [R,L]

Replace example.com by your domain name. If you are using www or any other subdomain, you need to include it as well.

For example, if your website uses www (https://www.example.com), the following line should be edited like this:

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

Please note, only a single RewriteEngine On is necessary. In case you already have that line on your .htaccess file, you may not need to add another.

Troubleshooting

If you can’t locate the .htaccess file of your website for some reason:

  • you could be looking at the wrong location.
  • the .htaccess file is probably hidden.
  • you may need to create it.

Conclusion

Your visitors are going to be redirected from HTTP to HTTPS from now on.

Did this article help you? Share it and help someone else to use HTTPS on their website.

Please rate this article to help us improve our Knowledge Base.

0 0