Force Site To Use Only HTTPS Print

  • ssl, force https
  • 1

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 ^(.*)$ https://www.myaccount.cc/$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$ https://www.myaccount.cc/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$ https://www.myaccount.cc/subdir/demo.html [R,L]


How To Force A Sub-Directory Through HTTPS

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




Was this answer helpful?

« Back