How to Force Redirect HTTPS using .htaccess (Updated 2023)

How to Force Redirect HTTPS using .htaccess (Updated 2023)

How to Force Redirect HTTPS using .htaccess (Updated 2023)

Ensuring secure connections is essential in today’s digital landscape. HTTPS has become a symbol of trust, with major browsers like Google Chrome and Firefox warning users when they visit websites that operate on HTTP, labeling them as unsecure. This article serves as a step-by-step guide for moderators to enforce HTTP to HTTPS redirection using the .htaccess file.

Editing the .htaccess File using FTP or cPanel To begin with, it’s important to know how to access and modify the .htaccess file. Here are three methods you can use:

  • Method 1: Editing the .htaccess file locally on your computer and uploading it to the server via FTP.
  • Method 2: Using an FTP program to remotely edit the .htaccess file.
  • Method 3: Accessing the cPanel File Manager to edit the .htaccess file.

Editing the .htaccess File in cPanel File Manager

  1. Access your cPanel account.
  2. Click on “File Manager” from the Files tab.

  1. Locate the “Settings” option at the top right corner of the page and click on it.
  2. Click on the “Document Root” for your domain name and ensure that the “Show Hidden
  3. Files” option is checked (refer to the image below for assistance).

How to Force Redirect HTTPS using .htaccess (Updated 2023)

  1. Save to update the File Manager settings.
  2. Find the “public_html” file and click on it.
  3. On the next page, you will see the .htaccess file.
  4. Click once on the .htaccess file, and then click on the “Edit” button in the top toolbar.
  5. A warning box may appear, but click on “Edit” to proceed.
  6. On the next page, a code editor will open, allowing you to modify the code.
  7. Edit the necessary code as required.
  8. Save the file to apply the changes.

Please note that it’s always recommended to create a backup of your site before making any changes to the .htaccess file. If any issues arise, you can restore the previous version of the file.

Redirect HTTP to HTTPS in Apache

Redirecting HTTP to HTTPS in Apache In this section, we’ll cover different scenarios for redirecting HTTP to HTTPS in an Apache server:

Option 1: Redirecting all web traffic

Add the following code below the existing code in your .htaccess file:

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

Option 2: Redirecting a specific domain

Use the following code to redirect a specific domain to HTTPS:
RewriteEngine On RewriteCond %{HTTP_HOST} ^yourdomain\\.com [NC] RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]

Option 3: Redirecting a specific folder

To redirect a particular folder to HTTPS, add the code below:
Services we offer:RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} folder
RewriteRule ^(.*)$ https://www.yourdomain.com/folder/$1 [R,L]
Note: Replace “yourdomain” with your actual domain name and “/folder” with the appropriate folder name.

HTTP to HTTPS redirect in NGINX

Redirecting HTTP to HTTPS in Nginx For Nginx web servers, follow these steps:
  1. Login to your Nginx webserver.
  2. Add the following line within the server directive: return 301 https://$server_name$request_uri.
  3. Save the file.
  4. Restart your Nginx web server.

Note: It’s advisable to create a backup of the nginx.conf or default.conf file before making any changes.

HTTP redirect to HTTPS in Cloudflare

Enabling HTTP Redirect to HTTPS in Cloudflare If you’re using Cloudflare, the process of enabling HTTP redirection is straightforward:

  1. Log in to your Cloudflare account.
  2. Select your website.
  3. Go to the Crypto tab.
  4. Scroll down to the “Always Use HTTPS” section.
  5. Ensure that it is turned on.

What is SSL

What is SSL?

Importance of SSL and HTTPS SSL (Secure Sockets Layer) is a standard security protocol that encrypts the connection between web browsers and servers. It protects sensitive information and offers several benefits:

  1. Secure Transactions: HTTPS is crucial for online stores to ensure secure transactions, protecting credit card information and preventing shopping scams.
  2. Boost SEO: SSL certificates positively impact SEO rankings, as search engines prioritize secure websites.
  3. PCI Compliance: E-commerce stores must be PCI compliant to enable credit card transactions and avoid penalties.
  4. Browser Trust Indicator: Loading a website over HTTPS displays a green bar, indicating trustworthiness and boosting user confidence.

In conclusion, having an SSL certificate and enforcing HTTPS on your website is imperative for online businesses. This article provided a comprehensive guide on how to force redirect HTTP to HTTPS using the .htaccess file. If you encounter any issues during the process, please leave a comment for further assistance.

Author: Tech Editor
Hello friends, I am a Technical Author & Founder of Tech Tamil. I am a Journalist. I enjoy learning things related to new technology and teaching others. I request you to keep supporting us like this, and we will keep providing further information for you. :)

Leave a Reply

Your email address will not be published. Required fields are marked *