How to force SSL using .htaccess

How to force SSL

Sometimes it's necessary to make sure your website’s visitors use the SSL encrypted connection.


Forcing visitors to use SSL can be accomplished through your .htaccess file using mod_rewrite.

To force all web traffic to use HTTPS insert the following lines of code in the .htaccess file in your website’s root folder. 

Important:If you have existing code in your .htacess, add this above where there are already rules with a similar starting prefix.

 

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.

To force a specific domain to use HTTPS, use the following lines of code in the .htaccess file in your website's root folder:

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

Make sure to replace example\.com with the domain name you're trying force to https. Additionally, you need to replace www.example.com with your actual domain name.

If you want to force SSL on a specific folder you can insert the code below into a .htaccess file placed in that specific folder:

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

Make sure you change the folder reference to the actual folder name. Then be sure to replace www.example.com/folderwith your actual domain name and folder you want to force the SSL on.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How to Install SSL Certificates

INSTALLING A THIRD PARTY SSL CERTIFICATE You can install an SSL certificate from a third-party...

What is a SSL Certificate?

What is a Secure Socket Layer (SSL) Certificate? SSL Certificates are small data files that...

Importance of SSL Certificates

Why Google is Forcing You To Have SSL Certificates on Your Websites Don’t have an SSL...

IMPORTANCE OF AN SSL CERTIFICATE

What is an SSL certificate?   An SSL(Secure Sockets Layer) certificate is a type of digital...