我开发了一个 cakephp 网站,所有页面都应使用 ssl。它按预期工作,除了当我在控制器中使用重定向时,它重定向到 http://subdomain.domain.com 而不是 https://subdomain.domain.com/controller/action。
我通过为指向 cakephp 应用程序的端口 80 创建虚拟主机并在 .htaccess 中添加这些重写规则解决了这个问题
RewriteCond %{HTTPS} 已关闭
重写规则 (.*)https://% https://%{HTTP_HOST}%{REQUEST_URI} [L]
这可以捕获这种情况并重定向到 https,但这会给服务器带来不必要的额外流量。
造成这种额外流量的原因是重定向功能,因为它会生成错误的网址。我研究了重定向函数,它调用 router::url 来创建实际的 url。但是我无法弄清楚如何或在哪里指示路由器使用 https 而不是 http。
Tim
我做了一些猜测,但我怀疑正是这个 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 才能使上述功能正常工作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)