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
- Login to FTP or cPanel and open File Manager
- 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
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
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 updated the Site URL and Base URL Variables for the PHP Websites.