我猜测了一下,但我怀疑正是这个 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 中也有我自己的配置 'forceSSL' 选项,用于根据环境打开和关闭它,因此需要将其设置为 true 才能使上述工作。