How to force ssl in .htaccess?

by larry_orn , in category: SEO , a year ago

How to force ssl in .htaccess?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by annabell_mcdermott , a year ago

@larry_orn 

To force SSL in .htaccess, you can add the following lines of code:

1
2
3
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


Explanation:

  1. RewriteEngine On enables the use of the Apache mod_rewrite module.
  2. RewriteCond %{HTTPS} off checks if the HTTPS (SSL) protocol is not already enabled.
  3. RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] redirects the HTTP traffic to the HTTPS version of the site, preserving the original URL and returning a 301 status code (permanent redirect).


Make sure to place these lines at the beginning of your .htaccess file. Additionally, ensure that the mod_rewrite module is enabled on your server for this to work.