How to redirect HTTP to HTTPS Using .htaccess

You are here:
Estimated reading time: 2 min

Chrome and Firefox have started showing insecure warnings on sites without SSL certificates. Without SSL, your website will show insecure to the visitors. Therefore, using an SSL-encrypted connection for safety, accessibility or PCI compliance reasons is necessary. It becomes very important to redirect from HTTP to HTTPS.

What is SSL?

SSL (Secure Sockets Layer) is a standard security protocol for establishing encrypted links between a web server and a browser in an online communication.

The usage of SSL technology ensures that all data transmitted between the web server and browser remains encrypted.

An SSL certificate is necessary to create SSL connection. You would need to give all details about the identity of your website and your company as and when you choose to activate SSL on your web server. Following this, two cryptographic keys are created — a Private Key and a Public Key.

In order to force your web traffic to use HTTPS, edit the codes in the .htaccess file.

Before we move onto redirecting HTTP to HTTPS, here’s how you can edit .htaccess file. If you already know skip to Redirection steps.

Editing .htaccess File

There are instructions/directives in the .htaccess file that tell the server how to act in certain scenarios and directly affects how your website functions. Common directives in .htaccess file:

  • +Redirects
  • +Rewriting URLs

Ways to edit an .htaccess file:

1. Edit the file on your computer and upload it to the server using FTP.

2. Use “Edit” mode in FTP program that allows you to edit a file remotely.

3. Use a text editor and SSH to edit the file.

4. Use the File Manager in cPanel to edit the file.

Editing .htaccess in cPanel File Manager

Note: Backup your website in case something goes wrong.

1. Login to cPanel

2. Files > File Manager > Document Root for:

3. Now select the domain name you want to access

4. Check “Show Hidden Files (dotfiles)”

5. Click “Go”

6. After a new tab or window opens, look for the .htaccess file.

7. Right click on the .htaccess file and click on “Code Edit” on the menu.

8. A dialogue box may pop up asking about encoding. Click “Edit” button to continue.

9. Edit the file

10. “Save Changes” when done.

11. Test your website to make sure it is done correctly. In case, there is an error, restore to the previous version and try again.

12. Once you are done, click “Close” to close the window.

 

Redirecting HTTP to HTTPS

Redirect All Web Traffic

1. If you have existing code in your .htaccess, add the following:

#Redirection code starts
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
#Redirection code Ends

2. Save the code.

 

 

 

Was this article helpful?
Dislike 1

Author

Joshua

Web Hosting Geek Technical Support Staff G Online Sites