If you are building CakePHP application and want to move all traffic to HTTPS you can follow bellow simple steps without making any change in .htaccess file
Step 1. Edit AppController to make change
1 |
use Cake\Event\Event; |
Step 2. Load security component
1 2 3 4 5 6 7 8 9 10 |
public function initialize() { parent::initialize(); $this->loadComponent('Security', ['blackHoleCallback' => 'forceSSL']); $this->loadComponent('RequestHandler'); $this->loadComponent('Flash'); $this->loadComponent('Cookie'); } |
Step 3. Create function which redirects all non https request to https
1 2 3 4 5 6 7 8 9 |
public function forceSSL(){ $this->request->addDetector('ssl', array( 'env' => 'HTTP_X_FORWARDED_PROTO', 'value' => 'https' )); if($_SERVER['HTTP_X_FORWARDED_PROTO'] == "http") { return $this->redirect('https://' . env('SERVER_NAME') . $this->here); } } |
Hope this will help you 🙂