问题描述
我开发了一个应该对所有页面使用 ssl 的 cakephp 站点.它按预期工作,除非我在控制器中使用重定向时它重定向到 http://subdomain.domain.com 而不是 https://subdomain.domain.com/controller/action.
I have developed a cakephp site that should use ssl for all pages. It works as expected except when I use redirect in a controller it redirects to http: //subdomain.domain.com not https: //subdomain.domain.com/controller/action.
我通过为指向 cakephp 应用程序的端口 80 创建一个虚拟主机并在 .htaccess 中添加这些重写规则解决了这个问题
I have solved this by creating a virtual host for port 80 pointing to the cakephp application and added these rewrite rules in .htaccess
RewriteCond %{HTTPS} 关闭RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
RewriteCond %{HTTPS} offRewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
这会捕捉到这种情况并重定向到 https,但这会给服务器带来不必要的额外流量.
This catches this situation and redirect to https but this gives unnecessary extra traffic to the server.
这种额外流量的原因是重定向功能,因为它会生成错误的网址.我查看了重定向函数,它调用 router::url 来创建实际的 url.但是,我无法弄清楚如何或在何处指示路由器使用 https 而不是 http.
The cause for this extra traffic is the redirect function as it generates wrong urls. I have looked into the redirect function and it calls router::url to create the actual url. However I am not able to figure out how or where to instruct the router to use https not http.
蒂姆
推荐答案
我发现错误是 Apache 配置错误.
I found the error it was a misconfiguration of Apache.
尝试 Jamies 解决方案后,该站点最终进入重定向循环,因为即使请求是 https,RequestHandler->isSSL() 也返回 false.然后我发现没有设置 $_SERVER['https'] 并且 $_SERVER['port'] 是 80,而不是预期的 443.
Trying out Jamies solution, the site ended up in a redirect loop, because RequestHandler->isSSL() returned false even if the request was https. I then discovered that the $_SERVER['https'] was not set and the $_SERVER['port'] was 80, not 443 as expected.
那时我已经将我的 ssl 指令放在了 sites-available/default 中,
At that point I have placed my ssl directives in sites-available/default,
SSLEngine on
SSLCertificateFile [path to cert file]
SSLCertificateKeyFile [path to keyfile]
将 ssl 指令移动到子域的虚拟主机解决了该问题,并带有重定向循环.
Moving the ssl directives to the virtual host for the subdomain resolved the issue, with redirect loops.
实际上也解决了我最初的问题,因为方法 Router::url 检查 $_SERVER['https'] 如果设置它会生成一个以 https 开头的 url: 否则只是 http:
Actually is also solved my initial problem because the method Router::url checks for $_SERVER['https'] if set it generates a url that starts with https: otherwise just http:
我已经使用 .htaccess 中的重写规则测试了 Jameies 解决方案和我自己的解决方案,并且在修复后它们都按预期工作.
I have tested both Jameies solution and my own with rewrite rules in .htaccess and they both work as expected after the fix.
蒂姆
这篇关于将 cakephp ssl 站点路由重定向到 http 而不是 https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!