cakephp2 force https redirect on windows server

35 Views Asked by At

I have a subdomain hosted on windows server developed by cakephp2.7 installed SSL on server, it is working if explicitly type https but do not automatically converts http to https I want to automatically convert any url to https

tried many things with .htaccess file but none work current htaccess

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteCond %{HTTPS} !on
 RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f    
 RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

also tried

public $components = array('Security');

public function beforeFilter() {
    $this->Security->blackHoleCallback = 'forceSSL';
    $this->Security->requireSecure();
}

// Add this function in your AppController
public function forceSSL() {
    return $this->redirect('https://' . env('SERVER_NAME') . $this->here);
}

with this code even any link click was not working.

1

There are 1 best solutions below

0
alamnaryab On

this is for other if stuck in the same issue I resolved my issue to force redirect to https by adding the following code in beforeFilter() method of `AppController' class

    //https redirect
    if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === "off") {
        $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        header('HTTP/1.1 301 Moved Permanently');
        header('Location: ' . $location);
        exit;
    }