How to Redirect HTTP to HTTPS Using .htaccess: A Complete Guide

Ensuring your website uses HTTPS is critical for security, trust, and SEO rankings. If your website is still accessible through HTTP, you risk data breaches and lower search engine visibility. In this article, we’ll explain how to redirect all HTTP traffic to HTTPS using the .htaccess file, ensuring that your site is secure and accessible only through HTTPS.


1. Why Redirect from HTTP to HTTPS?

Redirecting HTTP to HTTPS ensures secure communication between a user’s browser and your web server. Here are some key reasons:

a. Security

HTTPS encrypts data, protecting users’ personal and sensitive information from interception.

b. SEO Benefits

Google ranks HTTPS-enabled websites higher, giving them a competitive edge in search engine results.

c. User Trust

Browsers show a padlock symbol for HTTPS websites, signaling trustworthiness.

d. Compliance

Data protection laws like GDPR mandate secure data handling, which HTTPS ensures.


2. What Is the .htaccess File?

The .htaccess file is a configuration file used by Apache web servers. It allows you to control website behavior, including enabling redirects, setting security rules, and customizing error pages.


3. How to Access the .htaccess File

You can access the .htaccess file through:

a. cPanel

  1. Log in to your hosting account’s cPanel.
  2. Go to File Manager.
  3. Navigate to the public_html directory (or the root directory of your site).
  4. Make sure Show Hidden Files is enabled.
  5. Look for the .htaccess file.

b. FTP Client

  1. Use an FTP client like FileZilla.
  2. Connect to your server using your login credentials.
  3. Navigate to your website’s root directory.
  4. Ensure your FTP client displays hidden files.
  5. Download and edit the .htaccess file using a text editor.

Note: If the .htaccess file doesn’t exist, create one using a text editor and upload it to the root directory.


4. Backing Up the .htaccess File

Before making any changes, back up the .htaccess file to prevent site downtime in case of errors.

  1. Download a Copy: Save the existing file to your local machine.
  2. Rename the File: Use a name like htaccess_backup.txt for easy identification.

5. How to Redirect HTTP to HTTPS Using .htaccess

Add the following code to your .htaccess file to redirect all HTTP traffic to HTTPS:

Redirect Code for HTTPS

apacheCopy codeRewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Code Breakdown:

  • RewriteEngine On: Enables the URL rewriting module.
  • RewriteCond %{HTTPS} off: Checks if HTTPS is disabled.
  • RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]: Redirects the user to the HTTPS version using a 301 (permanent) redirect.

Redirect Code for a Specific Domain

If you want to redirect traffic to a specific domain, use this code:

apacheCopy codeRewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC]
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [L,R=301,NC]

Redirect Code for WWW to Non-WWW

apacheCopy codeRewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com [NC]
RewriteRule ^(.*)$ https://yourdomain.com/$1 [L,R=301,NC]

Redirect Code for Non-WWW to WWW

apacheCopy codeRewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC]
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [L,R=301,NC]

6. Testing the Redirect

After updating the .htaccess file, test the redirect by:

  1. Clearing Your Browser Cache: Avoid loading cached pages.
  2. Entering the HTTP URL: Type http://yourdomain.com in your browser.
  3. Checking the Redirect: Ensure the browser automatically redirects to the HTTPS version (https://yourdomain.com).
  4. Using Online Tools: Tools like Redirect Checker can verify if your redirects are working.

7. Troubleshooting Common Issues

a. 500 Internal Server Error

Cause: Incorrect syntax in the .htaccess file.
Solution: Restore the backup file and review the code for errors.

b. Redirect Loop Error

Cause: Multiple redirect rules causing an endless loop.
Solution: Remove duplicate or conflicting redirect rules.

c. Site Not Secure Warning

Cause: Missing or expired SSL certificate.
Solution: Ensure your SSL certificate is active and properly installed.


8. Additional Security Tips

  1. Use a Reliable SSL Provider: Ensure your SSL certificate is issued by a trusted provider like Let’s Encrypt.
  2. Enable HSTS: Add this line to your .htaccess file to force HTTPS:apacheCopy codeHeader always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
  3. Enable Firewall Protection: Use a web application firewall (WAF) to prevent attacks.
  4. Perform Regular Backups: Keep regular backups of your website files and database.

9. Conclusion

Redirecting HTTP to HTTPS using the .htaccess file is essential for website security, SEO performance, and user trust. Following this comprehensive guide ensures a smooth implementation, safeguarding your site’s integrity and online visibility.