21 Jan 2019

Force HTTPS on the Websites using .htaccess

Bought SSL for your domain and finished with the SSL installation? Now wondering how to force HTTPS on the Websites using .htaccess? Great find as your search will be complete with this guide.

Today we will share a few .htaccess rules that can be used to Force HTTPS on the website with www version and non-version as per the requirements. This guide can be followed for both PHP and HTML Based Websites.

Requirements

  • SSL Installed on the server for your domain
  • Permissions to edit .htaccess file
  • FTP or File Manager access in cPanel

Procedure

  1. Login to FTP or cPanel and open File Manager
  2. Create .htaccess inside the root directory of the website and follow the below steps as per www or non-www version requirements
For www version

Paste the following Lines of Code in .htaccess file and save it.

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

For non-www version

Paste the following Lines of Code in .htaccess file and save it.

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]

 

Note: Make sure that you have edited the PHP Config file and update Site URL and Base URL Variables for the PHP Websites.

You can follow this guide for Forcing HTTPS for WordPress Websites.

Force HTTPS for WordPress websites by .htaccess

If you are facing any kind of issues in HTTPS redirection, you can drop us an email at [email protected]