在cakephp ssl站点路由中重定向到http而不是https

1 ssl https redirect router cakephp

我开发了一个cakephp网站,应该对所有页面使用ssl.它按预期工作,除非我在控制器中使用重定向,它重定向到http://subdomain.domain.com而不是https://subdomain.domain.com/controller/action.

我通过为端口80创建指向cakephp应用程序的虚拟主机并在.htaccess中添加了这些重写规则来解决这个问题.

RewriteCond%{HTTPS} off RewriteRule(.*)https://% {HTTP_HOST}%{REQUEST_URI} [L]

这会捕获这种情况并重定向到https,但这会给服务器带来不必要的额外流量.

这种额外流量的原因是重定向功能,因为它生成了错误的URL.我查看了redirect函数,并调用router :: url来创建实际的url.但是,我无法弄清楚如何或在何处指示路由器使用https而不是http.

蒂姆

Jam*_*mie 5

我正在做一些猜测,但我怀疑这是一个非常糟糕的RewriteRule.

你应该"重定向"而不是"重写".Cake生成的链接通常与根相关,因此除非将"true"作为第二个参数传递,否则不要指定协议.

我也有apache监听80和443,这样我至少可以回应不正确的请求.

这是我在AppController类中执行相同操作的代码:

function beforeFilter() {
    parent::beforeFilter();
    $this->_setupSecurity();
}

function _setupSecurity() {
    $this->Security->blackHoleCallback = '_badRequest';
    if(Configure::read('forceSSL')) {
        $this->Security->requireSecure('*');
    }
}

/**
* The main SecurityComponent callback.
* Handles both missing SSL problems and general bad requests.
*/

function _badRequest() {
    if(Configure::read('forceSSL') && !$this->RequestHandler->isSSL()) {
        $this->_forceSSL();
    } else {
        $this->cakeError('error400');
    }
    exit;
}

/**
* Redirect to the same page, but with the https protocol and exit.
*/

function _forceSSL() {
    $this->redirect('https://' . env('SERVER_NAME') . $this->here);
    exit;
}
Run Code Online (Sandbox Code Playgroud)

我在bootstrap.php中也有自己的config'forceSSL'选项,用于根据环境打开和关闭它,因此需要将其设置为true才能使上述工作正常.