PHP - 将 HTTPS 重定向到 HTTP - 无限循环
我试图阻止通过 HTTPS 访问我网站上的某些页面,并且(无论出于何种原因)我想通过 PHP 而不是通过 .htaccess 来完成此操作。
这是我正在使用的代码:
if ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://mydomain.com");
}
但由于某种奇怪的原因,我陷入了无限循环,无法让它工作。当我检查 Firebug 中的响应标头时,我发现 location
标头设置为 https://mydomain.com
而不是 http://mydomain。 com
,这导致了无限循环。
编辑:直接访问http://mydomain.com
确实有效。
另请注意:如果我将它们发送到不同的页面,则此方法有效,但如果我将它们发送到同一页面,则此方法无效。因此,如果我在 mydomain.com/somePage.php
中运行上述代码,然后尝试通过 https://mydomain.com/somePage.php
访问它,它'将正确重定向到(非 SSL)主页。只有当我将它们重定向到具有不同协议的同一页面时,它才会忽略该协议。
我做错了什么?
I'm trying to prevent certain pages on my site from being accessed through HTTPS, and (for whatever reason) I want to do it through PHP and not through a .htaccess.
Here's the code I'm using:
if ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://mydomain.com");
}
But for some odd reason, I'm stuck in an infinite loop, and can't get it to work. When I check the response headers in firebug, I see that the location
header is set to https://mydomain.com
instead of http://mydomain.com
, which is causing the infinite loop.
EDIT: Accessing http://mydomain.com
directly does work.
Also note: this works if I send'em to a different page, but not if I send them to the same page. So if I run the above code in mydomain.com/somePage.php
, and then try accessing it through https://mydomain.com/somePage.php
, it'll properly redirect to (non-SSL-ed) homepage. Only when I redirect them to the same page with a different protocol does it ignore the protocol.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
事实证明我的代码没有任何问题。服务器的设置方式只是扰乱了我的标题。我正在使用 engineHosting.com,我不得不说:他们非常有帮助。经过大量的回访后,与他们一起,这是他们发给我的内容:
It turns out there was nothing wrong with my code. The server was just setup in a way that was messing with my headers. I'm using engineHosting.com, and I have to say: they were very helpful. After a lot of back & forth with them, here's what they sent me:
不完全确定,但我注意到以下几点:
如果您的 .htaccess 或服务器配置设置为坚持使用 HTTPS,您将无法在 php 级别解决该问题。
您省略了 http://mydomain.com 上的尾部斜杠,这会创建隐式重定向。尝试使用该位置的完整实际路径 - http://mydomain.com/index.html或 http://mydomain.com/index.php 例如。
Not completely sure, but here's a couple things I note:
If your .htaccess or server config it set to insist on HTTPS, you won't be able to get around that at the php level.
you left off the trailing slash on http://mydomain.com which creates an implied redirect. Try it with the full actual path in the location -- http://mydomain.com/index.html or http://mydomain.com/index.php for example.
我有一个运行 HTTPS 站点的自托管服务器。我做了一些快速测试,您的代码完全按照预期工作。这是我的代码(逐字记录,仅更改了域):
redir.php
我肯定会说 - 正如 Jared Farrish 在他的聊天中所说 - 这是主机的问题。他们的服务器配置中的某些内容强制重定向回 HTTPS。我不认为这是 PHP 的错误。我的服务器正在运行 PHP 5.3.5 和 Apache 2.2.17。
I have a self-hosted server running an HTTPS site. I did a few quick tests, and your code works exactly as expected. Here's my code (verbatim, with only the domain changed):
redir.php
I would definitely say - as Jared Farrish said in his chat - that it's an issue with the host. Something in their server configuration is forcing the redirect back to HTTPS. I don't think it's a PHP bug. My server is running PHP 5.3.5 with Apache 2.2.17.