How to Force a Dedicated SSL Using the .htaccess File

Home Links

IMPORTANT: If you have existing code in your .htaccess, add the code mentioned in this article, above where there are already rules with a similar starting prefix.

Force All Web Traffic

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

RewriteEngine On
      RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
      RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
      RewriteCond %{SERVER_PORT} 80 
      RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

Force Specific Domain

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 %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
      RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
      RewriteCond %{HTTP_HOST} ^example\.com [NC]
      RewriteCond %{SERVER_PORT} 80 
      RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]