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
October 27, 2026

Fixing the “cagefs_enter: Unable to fork” Error in cPanel/WHM

Few messages are as disruptive on a shared hosting server as cagefs_enter: Unable to fork. Terminal sessions refuse to open,…

October 26, 2026

Why Is My WordPress Website Slow? 10 Common Causes and Easy Fixes

Why Is My WordPress Website Slow? 10 Causes and Fixes (2026 Guide) Is your WordPress site loading slowly? Discover the…

October 25, 2026

How to Fix 403 Forbidden Error in cPanel!

Have you opened your website only to see a 403 Forbidden error? This error can be frustrating because it prevents…

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
Back To Top