获取完整的URL在PHP重定向到SSL [英] Get full URL in PHP to redirect to SSL
问题描述
我有我需要强制SSL一些PHP页面。我不想与mod_rewrite的或类似的东西做到这一点,我想保持所有的逻辑在PHP。现在我有code,看起来像这样:
I have some PHP pages that I need to force SSL for. I don't want to do it with mod_rewrite or anything like that, I want to keep all the logic in PHP. Right now I have code that looks like this:
if($_SERVER["HTTPS"] != "on") {
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]);
exit();
}
由于服务器名称是通用的,因为我托管多个站点,不同的域名,然而,这并不正常工作。所以SERVER_NAME是WWW。以上code会重定向到的https://www/index.php 这是无效的。
This does not work however because the server name is generic as I am hosting multiple sites with different domain names. So the SERVER_NAME is "www". The above code will redirect to https://www/index.php which is not valid.
我已经试过的print_r($ _ SERVER)来查看可用的变量我,但他们都不给我完整的URI请求(http://example.com/index.php),以及下一个最接近的选项,我可以看到而不是SERVER_NAME是SERVER_ADDR这也不会帮我,因为要去 https://127.0.0.1/index.php 必去的默认的Apache网站并没有给定义example.com(更不用提我的SSL证书将不再有效)。
I have tried print_r($_SERVER) to see the variables available to me but none of them give me the full URI request (http://example.com/index.php), and the next closest option I can see instead of SERVER_NAME is SERVER_ADDR which will also not help me, because going to https://127.0.0.1/index.php will go to the default apache site and not to the definition for "example.com" (not to mention that my SSL cert would no longer be valid).
有什么建议?
推荐答案
在最后我不得不谁管理Apache的代理服务器来添加家伙代理preserveHost在
到apache2.conf,这使HTTP_HOST(和SERVER_NAME)的正确转发到后端的Apache服务器。
In the end I had the guy who manages the Apache proxy server to add ProxyPreserveHost On
to apache2.conf and this enabled forwarding of HTTP_HOST (and SERVER_NAME) properly to the backend Apache server.
感谢大家对你的建议,我很抱歉,这个问题最终被一些愚蠢的事,这不是即使在我的问题的范围。如果我工作的Apache服务器没有在它前面的代理,你的大部分建议都已经被证明完全正确的。
Thank you everyone for your suggestions, I'm sorry the problem ended up being something stupid that wasn't even in the scope of my question. If the Apache server I was working on did not have a proxy in front of it, most of your suggestions would have proven completely accurate.
这篇关于获取完整的URL在PHP重定向到SSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!