A complete guide on how to redirect HTTP to HTTPS using .htaccess file.

A complete guide on how to redirect HTTP to HTTPS using .htaccess file.

Installing an SSL certificate on your website is very important in this age of technology as security breaching becomes rampant. If you have ever been troubled or wondered why your website still shows the insecure icon even after you install an SSL certificate on your web server, this is a tutorial for you. Let's start with the concept of what an SSL is.

What is an SSL?

SSL (Secure Sockets Layer) is a cryptographic protocol that provides standard security between two machines or devices operating over the internet or an internal network. An SSL establishes a secure and encrypted connection between a web server and a browser in online communication.

What do you need to establish security on your website?

In order to have an SSL technology running on your web server, you need to have/purchase an SSL certificate. Installing this certificate on your web server makes it possible to turn your website's address from HTTP to HTTPS, the ‘S’ standing for ‘secure’.

Learn more: What is an SSL?

How to force redirect HTTP to HTTPS.

force-ssl-http-https-contents.jpg

In this section, I will guide you step by step on how to force redirect your web traffic from HTTP to HTTPS using the .htaccess file. Let’s get started:

Accessing & Editing .htaccess in cPanel File Manager

Note: I recommend you back up your website's file in case anything goes wrong while editing.

  1. First, log in to cPanel
  2. Navigate File Manager > public_html
  3. Now select the domain name you want to access (If having multiple hosting domains or using subdomains).
  4. If you can't see the .htaccess file there, then click on the setting icon on the top right corner.
  5. Then Check “Show Hidden Files (dotfiles).”
  6. Click “Go.”
  7. Right-click on the .htaccess file and click on “Code Edit” on the menu.
  8. You will then see a pop-up asking about encoding. Click the “Edit” button to proceed.
  9. Edit the file.

    - Redirecting All Web Traffic

    Insert the following code at the bottom or top of the .htaccess file. Avoid tampering with the existing code. Ensure you replace www.yourdomain.com with your website's link.

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

    - Redirecting To Only a Specific Domain

    If you want a specific domain to use HTTPS, insert the following codes to the .htaccess file:

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

    Ensure you replace www.yourdomain.com with your domain link.

  10. Click on “Save Changes” when done.

  11. Check your website to see if it is done correctly. Your website should automatically have/redirect to https://
  12. If there is an error, restore to the previous version and try again.

If you find this guide helpful, kindly let me know your thoughts in the comment section. Help share with others having issues with redirecting HTTP to HTTPS.

Happy Coding!👏🥳