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

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

RewriteCond%{HTTPS}已关闭
RewriteRule(。*)https://% {HTTP_HOST}%{REQUEST_URI} [L]

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

产生额外流量的原因是重定向功能,因为它生成了错误的URL。我已经研究了重定向功能,它调用router :: url来创建实际的url。但是我无法弄清楚如何或在哪里指示路由器使用https而不是http。

提姆

最佳答案

我有点猜测,但是我怀疑正是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;
}


我在bootstrap.php中也有我自己的config'forceSSL'选项,用于根据环境启用和禁用此选项,因此需要将其设置为true才能使上述功能起作用。

关于cakephp - 在cakephp ssl网站路由中重定向到http而不是https,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4472648/

10-14 12:02
查看更多