I'd like to implement HTTPS for all pages.
I use CakePHP 2.3 with the "Auth" component.
Actually the only one way I found is to add a "beforeFilter-condition".
But this is very dirty because I have a lot of "not-SSL" requests because of Auth-Component.
AppController.php --->
class AppController extends Controller {
public function beforeFilter() {
if (env("SERVER_PORT") != "443") {
$this->Security->blackHoleCallback = 'forceSSL';
$this->Security->requireSecure();
}
}
public function forceSSL() {
$this->redirect('https://' . env('SERVER_NAME') . $this->here);
}
}
The problem appears when I am not logged in and I try to access my website
- Request --> Response (because of)
- GET hxxp://MYSITE/ --> 302 hxxps://MYSITE/ (beforeFilter-redirection)
- GET hxxps://MYSITE/ --> 302 hxxp://MYSITE/users/login (Auth component)
- GET hxxp://MYSITE/users/login --> 302 hxxps://MYSITE/users/login (beforeFilter-redirection)
- GET hxxps://MYSITE/users/login --> 200
- POST hxxps://MYSITE/users/login (with creds) --> 302 hxxp://MYSITE/ (Auth component)
- GET hxxp://MYSITE/ --> 302 hxxps://MYSITE/
- GET hxxps://MYSITE/ --> 200
SO, do you know another way to do that.
NB: I had to force secure my cookie in core.php because they weren't.
core.php --->
Configure::write('Session', array(
'defaults' => 'php',
'ini' => array(
'session.cookie_secure' => true
)));
Note that I also tried to force SSL by modifying .htaccess but all I get is infinite loop.
EDIT :
The default .htaccess in CakePHP is
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
What I tried to add :
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]