How to force HTTPS using the .htaccess file

How to force HTTPS using the .htaccess file

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.

    • Related Articles

    • CMS Brute Force Protection - htaccess

      Content management systems (CMS) such as Wordpress and Joomla! have become regular targets of brute force attempts. The most common way these types of attacks are performed is by hitting the wp-admin.php and administrator/index.php with thousands of ...
    • Force SSL with htaccess on linux

      if you already have a .htaccess just edit it using the same command above. Add the following contents to it: SSLOptions +StrictRequire SSLRequireSSL SSLRequire %{HTTP_HOST} eq "domainname.com" Save and exit. Make sure that the permission ...
    • Force SSL with htaccess on linux

      if you already have a .htaccess just edit it using the same command above. Add the following contents to it: SSLOptions +StrictRequire SSLRequireSSL SSLRequire %{HTTP_HOST} eq "domainname.com" Save and exit. Make sure that the permission ...
    • Common .htaccess Redirects

      #301 Redirects for .htaccess #Redirect a single page: Redirect 301 /pagename.php http://www.domain.com/pagename.html #Redirect an entire site: Redirect 301 / http://www.domain.com/ #Redirect an entire site to a sub folder Redirect 301 / ...
    • Common .htaccess Redirects

      #301 Redirects for .htaccess #Redirect a single page: Redirect 301 /pagename.php http://www.domain.com/pagename.html #Redirect an entire site: Redirect 301 / http://www.domain.com/ #Redirect an entire site to a sub folder Redirect 301 / ...