Forcing HTTPS with .htaccess

Forcing HTTPS with .htaccess

Often it can be beneficial to force your website visitors access your site using an SSL-encrypted connection (https) rather than the standard http connection. These reasons could be for security or to ensure that your site is meeting the recently updated Google ranking rules where your ranking will be negatively affected by user not assessing you site via https. whatever your need, the following guide will should you how to ensure that all of your visitors are forced to use https by editing your server .htaccess file.

To start, you will need to have full FTP access to the root directory if your web server. Assuming you have that, login and view the root directory, you will see a .htaccess file as well as a bunch of other site server and website related files.

Next, open the .htaccess file is a code editor (our preference is notepad++). We will show you three different options: all traffic, a specific domain, and a specific folder.

Forcing all website traffic to use HTTPS

It is likely you will have existing code in this file. If so, you need to add add this above the exiting rules with a similar starting prefix are located. If not, simply insert this code into the file.

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

**Be sure to replace www.example.com with your actual domain name.

Forcing a specific domain to use HTTPS

If you want to force a specific domain to use HTTPS, use the following lines of code:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

**Be sure to replace example\.com with the domain name you’re trying force to https. Also, replace www.example.com with your actual domain name.

Forcing SSL on a specific folder

If you want to force SSL on a specific folder, you need to oped that folder and create a new .htaccess file  (or edit if it already exists) and use the following lines of code into the .htaccess – remember, this .htaccess file is placed in that specific folder:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} folder 
RewriteRule ^(.*)$ https://www.example.com/folder/$1 [R,L]

**Be sure to change the folder reference to the actual folder name. Also,  replace www.example.com/folder with your actual domain name and folder name.

Top Tip for WordPress users: If you are using WordPress, insert this code above the # BEGIN WordPress line. 

Leave a Reply