force https protocol force https protocol codeigniter codeigniter

force https protocol


I've used this in the past, in a .htaccess file

RewriteEngine OnRewriteCond %{HTTPS} offRewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

There may be better implementations but it's always worked for me

If you want to remove the www part I've used this

RewriteCond %{HTTP_HOST} ^www\.(.+)RewriteCond %{HTTPS}s/%1 ^(on(s)|offs)/(.+)RewriteRule ^ http%2://%3%{REQUEST_URI} [L,R=301]

Here's the combined rule

RewriteEngine OnRewriteCond %{HTTPS} offRewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}RewriteCond %{HTTP_HOST} ^www\.(.+)RewriteCond %{HTTPS}s/%1 ^(on(s)|offs)/(.+)RewriteRule ^ http%2://%3%{REQUEST_URI} [L,R=301]

This deals with the protocol first and then removes the w's

Update:

To enforce https protocol for particular directories you could use something like

RewriteEngine OnRewriteCond %{HTTPS} offRewriteCond ^(login|register|userportal)RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

You would of course remove this (below) if you use the above

RewriteEngine OnRewriteCond %{HTTPS} offRewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}