How do I redirect all HTTP traffic to HTTPS using .htaccess?
Asked on Sep 03, 2025
Answer
To redirect all HTTP traffic to HTTPS using .htaccess, you need to add specific rewrite rules to your .htaccess file. These rules ensure that any request made over HTTP is redirected to the secure HTTPS version of your site.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Additional Comment:
- Ensure that the mod_rewrite module is enabled on your Apache server for these rules to work.
- The "R=301" flag indicates a permanent redirect, which is beneficial for SEO.
- Place this code at the top of your .htaccess file to ensure it executes before other rules.
- Always back up your .htaccess file before making changes to prevent site disruptions.
Recommended Links: