PHP:404 header 在 HTTPS 站点上不起作用 - 返回空白页面而不是默认的未找到页面

标签 php ssl https http-status-code-404

我有一个通配符证书,我们称之为 *.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/

相关文章:

javascript - 添加 auto_increment id 列后无法使用 php 和 javascript 将数据添加到 mysql 数据库

eclipse - 无法验证 SSL 主机

ssl - CloudFlare 灵活的 SSL 更改字体

.htaccess - 我应该如何摆脱使用 .htaccess 访问 https 中的停放域时的 SSL 错误?

php - 如何统计WordPress中每个父术语下的子术语数量

php - Yii2打开应用报错怎么解决

wordpress - 在本地环境的 https wordpress 网站上工作但无法连接

php - 如何在 PHP 中通过 HTTPS 发出 POST 请求?

node.js - 如何制作socket io | node.js - 使用 SSL

php - 如何使用explode implode从数据库中选中和取消选中复选框