我有一个通配符证书,我们称之为 *.example.com,我已经在 Apache 中为 *.example.com 设置了一个虚拟主机(用于 HTTPS 和非 HTTPS 流量)。
一直在搞一些 404 响应头,但我似乎遇到了问题,所以我做了一个简单的测试用例:
在虚拟主机中,我将所有流量重写为 index.php:
RewriteRule .* /index.php [QSA]
在 index.php
中,我返回一个 404 header :
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");
exit();
除此之外我没有做任何其他事情,所以没有设置 ErrorDocument
或类似内容。
服务器返回的 header 转储(用户 file_get_contents("https://test.example.com");
和 print_r($http_response_header);
来自另一个PHP 文件:
Array
(
[0] => HTTP/1.0 404 Not Found
[1] => Date: Mon, 08 Oct 2012 12:41:28 GMT
[2] => Server: Apache/2.2.16 (Debian)
[3] => X-Powered-By: PHP/5.3.3-7+squeeze14
[4] => Vary: Accept-Encoding
[5] => Content-Length: 57
[6] => Connection: close
[7] => Content-Type: text/html
)
如果我然后在任何子域/任何路径上输入 HTTP 站点,浏览器将正确显示默认的未找到页面,但如果我使用 HTTPS,浏览器将显示空白页面(IE 除外,它会正确(?)显示未找到的页面)。
header("Status: 404 Not Found");
也不起作用。
这不可能吧?我在这里遗漏了什么 - 可能是对 404 header 和通配符证书的某种限制?
应 Hakre 的要求更新
从 headers_list()
转储的 header :
Array
(
[0] => X-Powered-By: PHP/5.3.3-7+squeeze14
)
似乎服务器完全忽略了添加我的 404 header ,无论使用以下任何一种编写 header 的方式:
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");
header("HTTP/1.0 404 Not Found");
header("HTTP/1.1 404 Not Found");
其他 header ,如 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
正常工作。
最佳答案
这是错误的:
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");
即使你使用的是 HTTPS,你也应该返回:
header("HTTP/1.1 404 Not Found");
关于PHP:404 header 在 HTTPS 站点上不起作用 - 返回空白页面而不是默认的未找到页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12780822/