How to force HTTPS encryption when using an SSL

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

To force all web traffic to use HTTPS:

Insert the followng lines of code in the .htaccess file in your website's root folder:

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

Be sure to replace www.yourdomain.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.yourdomain.com/$1 [R,L]

Make sure to replace example\.com with the domain name you're trying to force to use HTTPS.

Additionally, you need to replace www.yourdomain.com with your actual domain name.

To force a specific folder to use HTTPS:

Insert the code below into an .htaccess file placed in that specific folder:

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

Make sure you change the folder to the actual folder name. Then replace www.yourdomain.com/folder with your actual domain name and the name of the folder you want to force to use HTTPS.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

PHP 5.4 vs 5.3 differences : What to watch out for

Each major release of any framework can make someone begin to second-guess what is still...

How to use the PHP mail function

You can use the PHP mail() function to send an email with PHP. The simplest way to do this is...

Parsing PHP code within HTML pages

The easiest way to parse or run PHP code within an HTML page is to simply change the extension...

Change/Select PHP version

If you require a different version of PHP than which we run by default you can select which...