How to force SSL/TLS (HTTPS)

How to force SSL/TLS

Method 1: Using .htaccess file
Step 1.
Open a FTP connection to your web server (or open file browser if hosted locally)

Step 2.
Navigate to your website's root directory. Typically "www" or "public_html"

Step 3.
Open and edit your .htaccess file (which should be inside "www" or "public_html"

Step 4.
Paste the following code into the beginning of the file

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Method 2: Using PHP (Inside the file)
Step 1.
Navigate to the html tag "<head>" and enter a new line.

Step 2.
Paste the following code

if($_SERVER["HTTPS"] != "on")
header("Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);

Was this answer helpful? 3 Users Found This Useful (3 Votes)