How to redirect HTTP to HTTPS through .htaccess?
To redirect HTTP to HTTPS through .htaccess, you can add the following code to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Explanation of the code:
RewriteEngine On
: Enables the use of mod_rewrite for URL rewritingRewriteCond %{HTTPS} off
: Checks if HTTPS is not onRewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
: Redirects the request to the same URL, but with HTTPS and a 301 redirect status code. The[L,R=301]
flags tell Apache to stop processing further rules and to send a permanent (301) redirect status code to the client.
Make sure to place this code at the beginning of your .htaccess file and test it to ensure it is working properly.