How do I force SSL using .htaccess

Forcing SSL for your site can be accomplished numerous ways depending on your content but one of the most common is by using mod_rewrite within your .htaccess file.

To force all web traffic to use HTTPS [SSL] you can insert the following lines of code into the .htaccess file in the root folder of your website.  It is important that you insert this above any pre-existing mod_rewrite code - placing it at the top of the file is usually suggested.  You will also want to make sure to replace "example.com" with your actual domain name.

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
Should you wish to only force a specific domain to HTTPS you would use the following code - making sure to replace "example\.com" with your actual domain name.  The back slash "\" before the period is important.
RewriteEngine On 
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
If you force SSL using these .htaccess rules and you end up with an "endless redirection", "redirection loop", or similar it is quite likely your script/software is trying to re-direct back to non-ssl which is then being re-directed by .htaccess back to SSL.  In this case you will likely need to configure SSL usage within your script/software directly.
  • 6 Users Found This Useful
Was this answer helpful?

Related Articles

How do I get an SSL certificate and how do I use it?

Free Options include cPanel's AutoSSL and Let's Encrypt.There are a few ways to get an SSL...

How do I obtain a CSR (Certificate Signing Request)?

There are two ways to generate a CSR depending on which type of account you have and both methods...

Cannot issue Let's Encrypt Certificate or Renewal Failures

Let's Encrypt is an amazing tool that allows you to issue free valid SSL Certificates on the...

How do I order an IP address?

Due to the Global IP Address shortage and strict ARIN IP justification guidelines, we no longer...

I installed an SSL Certificate but my site isn't automatically secure or forcing https://

Installing an SSL Certificate makes it possible to connect to your site using SSL over port 443...