Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP - Selective SSL

How do I force HTTPS for certain parts of a site, e.g. a login page or register page, and use HTTP for the rest of the site?

like image 294
freshest Avatar asked Nov 18 '11 12:11

freshest


1 Answers

My favorite convert to https forcing method is to put this as the first thing in your php script. It works in Joomla, and may very well work in CakePHP.

if( $_SERVER['SERVER_PORT'] == 80) {           
header('Location:https://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/'.basename($_SERVER['PHP_SELF'])); 
die();
}

This snippet will force https on whatever page you are viewing. If you want to isolate certian pages, just put some conditions based on the information in the "$_SERVER['PHP_SELF']" variable.

Otherwise, modify the .htaccess file, assuming your host allows you access to this:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} somefolder 
RewriteRule ^(.*)$ https://www.domain.com/somefolder/$1 [R,L]
like image 75
Mike_K Avatar answered Nov 02 '22 10:11

Mike_K