How to Automatically Force HTTP to Secure HTTPS Using .htaccess
Key Takeaway: A single copy-paste .htaccess snippet to automatically redirect all non-secure HTTP traffic to secure HTTPS.
Why Automatic HTTPS Redirection is Needed
Even after installing an SSL certificate, visitors typing yourdomain.com may still connect via insecure HTTP unless your web server forces an automatic 301 Permanent Redirect to the HTTPS protocol.
Step-by-Step .htaccess Configuration
- Log into your cPanel and open File Manager.
- Inside the
public_html/directory, click Settings in the top-right corner and ensure Show Hidden Files (dotfiles) is ticked. - Locate the
.htaccessfile (create one if it does not exist) and click Edit. - Add the following code block at the very top of the file:
# Force HTTPS Rewrite Rule
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Save changes and test by opening your website with http:// in a private browsing window. It will seamlessly redirect to https:// with the secure padlock active.