How to Force HTTPS Using the .htaccess

November 13, 2017

Long story short, I purchased a domain and hosted it with FastComet a week ago. After setting up the SSL/TLS, I tried to force all web traffic to use HTTPS. One way of achieving this would be adding the following code in the .htaccess file in the website’s root folder.

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Code that does the same job but written in slightly different syntax:

RewriteEngine on 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [L,R=301]

Further Reading:


Apache Documentations: