skip to Main Content

How to force SSL with .htaccess file?

Force SSL

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.

Latest Articles
April 13, 2024

How to create support ticket in Amar Hoster

আপনার হোস্টিং/ওয়েবসাইট এর কোনো টেকনিক্যাল সমস্যা হলে আমাদের টিকিট করে জানাতে পারেন। টেকনিক্যাল বিষয় চেক করতে কিছু সময় লাগতে পারে…

Subscribe To Our Newsletter

Don't get left out of the loop, make sure you subscribe to our newsletter below so you can be notified of our latest insights, tips, tutorials, sales and more!

Discussion