← Back to Home

How to Redirect HTTP to HTTPS Using .htaccess

How to Redirect HTTP to HTTPS Using .htaccess

Posted by NetworkWhois on

Moving your website from HTTP to HTTPS is important for security and SEO. I recently helped several clients with this transition and wanted to share the exact process I use to set up HTTPS redirection properly using .htaccess in cPanel.

Step-by-Step Guide

1. Access Your .htaccess File

First, log into your cPanel account and open the File Manager. Your .htaccess file should be in the root directory (public_html). If you don't see it right away, click on "Settings" and check the box for "Show Hidden Files" - files starting with a dot are hidden by default.

Show-hidden-files.png

2. Create or Edit .htaccess

Right-click on the .htaccess file and select "Edit". If you don't have one yet, you can create it. Before making any changes, I always recommend backing up the existing file - just in case!

How-to-Redirect-HTTP-to-HTTPS.png

3. Add the Redirection Code

Here's the code I use for redirecting all HTTP traffic to HTTPS:

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

Remember to replace "yourdomain.com" with your actual domain name!

Pro Tip: After implementing the redirect, clear your browser cache and test the site using different browsers to ensure everything works smoothly. I usually check both www and non-www versions of the site.

What This Code Actually Does

Let me break down how this redirection works:

  • RewriteEngine On activates Apache's rewriting capabilities
  • The conditions check if someone's accessing your site through HTTP (port 80)
  • The RewriteRule then redirects them to the HTTPS version of the same page they were trying to access

Troubleshooting Common Issues

If you run into problems after implementing this redirect, check these common issues:

  • Make sure your SSL certificate is properly installed
  • Verify that you have mod_rewrite enabled on your server
  • Double-check that you've replaced "yourdomain.com" with your actual domain

After implementing these changes, your website will automatically redirect all HTTP traffic to HTTPS, providing a more secure experience for your visitors.