Force Site To Use Only HTTPS

With Google pushing hard for sites to use https, and security for websites and logins becoming more important everyday there will come a time where you will want to force your website to be accesible only through https. An easy and flexible way to achieve this is through the use of .htaccess rules. The following bit of code will force your entire site through https only.

Force Entire Site Through HTTPS
RewriteEngine On

RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^myaccount\.cc$ [OR]
RewriteCond %{HTTP_HOST} ^www\.myaccount\.cc$
RewriteRule ^(.*)$$1 [R,L]

This will work regardless if you have your site using www or no www.

Note: This same function is also available direct in your control panel. if you would rather use that method please read Plesk 301 Redirect (https).

How To Force A Single Page Through HTTPS

At times you may only want to force a single page to use https, such as a login page. Here is how you can accomplish this:

RewriteEngine On 
RewriteCond %{HTTPS} !=on
RewriteRule ^demo\.html$ [R,L]

If the page is in a sub-directory then this code would change to something like:

RewriteEngine On 
RewriteCond %{HTTPS} !=on
RewriteRule ^demo\.html$ [R,L]

How To Force A Sub-Directory Through HTTPS

RewriteEngine On 
RewriteCond %{HTTPS} !=on
RewriteRule ^/?subdir/(.*)$1 [R,L]

  • 1 Users Found This Useful
Was this answer helpful?

Related Articles

Using .htaccess file to block IP addresses

Create the .htaccess file in your domain's directory. Add the following to your .htaccess file:...

Use of .htaccess

What is a .htaccess file?A .htaccess file is a type of Apache(web server) configuration file. It...

Enable Gzip Compression

Gzip compression is enabled by adding some code to  .htaccess file. Open the file and add the...

Redirecting to

This redirection will be done using Apache's mod_rewrite with the .htaccess file. You will need...