【问题标题】:PHP returns 200 OK status header instead of 404 error over SSL [closed]PHP通过SSL返回200 OK状态标头而不是404错误[关闭]
【发布时间】:2017-12-08 19:05:47
【问题描述】:

我有这个 php 脚本 (test.php),它检查页面是否存在于 mysql 中,它应该返回 404 状态标头,但它返回 200 OK 状态。这会导致控制台出现软 404 错误。

我试图修复它,但它根本不起作用。这是我的.htaccess。几个月前我将我的网站迁移到 SSL,我将所有流量从端口 80 重定向到 https。

RewriteEngine On
ErrorDocument 404 /404.shtml

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

RewriteEngine on
RewriteRule ^([0-9]+)-([0-9]+)-([0-9]+)/pressrelease([0-9]+)\.htm$ test.php?pressid=$4 [L]

这是我的 test.php

<?php 
include_once('dbc.php');       

$pid = strip_tags(mysql_real_escape_string($_GET['pressid']));

$rs_dirs = mysql_query("select id,name from categories order by name") or die(mysql_error());

$rs_press = mysql_query("select *
                             from press 
                             where id = '$pid'
                             and approved='1'
                             ") or die(mysql_error());;

$total = mysql_num_rows($rs_press);


if (mysql_num_rows($rs_press) == 0) {

header("HTTP/1.1 404 Not Found");
include '404.shtml';
die();
}

这里有什么问题,我该如何解决?

【问题讨论】:

标签: php .htaccess http-status-code-404


【解决方案1】:

您没有将实际状态码设置为 404。

查看header函数的用法:

http://php.net/manual/en/function.header.php

void header ( string $string [, bool $replace = true [, int $http_response_code ]] )

你的代码应该是:

header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found", true, 404);

还有一个替代功能:

https://secure.php.net/manual/en/function.http-response-code.php

http_response_code(404);

我更喜欢使用上述功能。因为它更详细地说明了您的意图。

【讨论】:

  • 对不起,不工作。我看到一个空白页。
【解决方案2】:

在您的代码中使用此代码:

if (mysql_num_rows($rs_press) == 0) {
   http_response_code(404);
   //and another staff here
}

【讨论】:

  • 不工作。我看到一个空白页。
  • 您是否包含了您的 shtml 或 echo 任何内容?在浏览器中查看网络。它应该显示 404 not found
  • 我检查了代码,但没有回显,但我认为问题出在 htaccess 重定向或标头在某处泄漏。
猜你喜欢
  • 2016-02-10
  • 1970-01-01
  • 1970-01-01
  • 2010-09-25
  • 2019-10-29
  • 2015-01-04
  • 1970-01-01
  • 2013-01-09
  • 1970-01-01
相关资源
最近更新 更多