How to force SSL with .htaccess ?
Using HTTPS is essential for protecting your website and your visitors’ data. If you have an SSL certificate installed on your website, you can force all traffic to use HTTPS by adding the following lines to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This code will redirect all HTTP traffic to HTTPS, with a permanent 301 redirect. This means that visitors who try to access your website using HTTP will be automatically redirected to the HTTPS version.
Here is a breakdown of what each line of code does:
RewriteEngine On
: Turns on the mod_rewrite module, which is required for URL rewriting.RewriteCond %{HTTPS} off
: Checks if the current connection is HTTP.RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
: Redirects the visitor to the HTTPS version of the current URL, with a permanent 301 redirect.
To use this code, simply add it to your .htaccess file. The .htaccess file is located in the root directory of your website.
Once you have added the code, save your .htaccess file and upload it to your server. Your website will now be forced to use HTTPS.
Please note: If you have any existing code in your .htaccess file, be sure to add the above code above it.