发送 http-404 响应但不在 IIS 服务器中发送自定义错误页面设置 | PHP

Send http-404 response BUT dont send custom error page setted in IIS server | PHP

让我们了解场景:

:- 使用 PHP 用于 IIS 8.5 上的 Web 应用程序

:- 使用以下 web-config 片段为 404 设置自定义错误页面:

<httpErrors errorMode="Custom" existingResponse="Replace">
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" path="/path/404.php" responseMode="ExecuteURL" />
</httpErrors>

:- 这对 non-existing 页面 和 IIS 也有效 return 这个 404.php 如果我将 header 设置为任何 PHP 页面中的 404(通过 header('HTTP/1.0 404 Not Found', true, 404); die;)。

问题:

现在,我想在任何特定条件下 php-file 中发送 404 HTTP 响应代码,假设数据库 return 有 0 行。我可以通过如上所述设置 header 来做到这一点,但这也会 return 自定义错误页面。 有没有办法发送 404 代码并告诉 IIS 按原样发送响应,而不连同它一起发送自定义错误页面?

问题:

:- 如果我删除自定义错误页面并将内容放在 header()die() 之间,那么它将满足我的要求,但 IIS 将无法在实际上不符合要求的页面上提供自定义错误存在。

:- 我正在使用 URL 重写,因此不能使用基于 location 标记的多个自定义错误页面,并且不想重定向到任何建议的不同 URL。

因为我有一个棘手的问题 solution/idea,我也想分享一下。但如果有人有更好的解决方案,我欢迎并感谢他们。

Trick/Solution/Idea:

Step 1:- From any existing php-page set a session variable just before header('HTTP/1.0 404 Not Found', true, 404); and put a specific message in that variable(I suggest to use different integer code as session variable's value for different php-pages) THEN set header and call die(); or exit;.

Step 2:- After that IIS or any other server will execute custom error page. SO, in custom error page check the session variable and echo content as follows....

........2a - If session variable is not set then display "normal error content with main menu" etc.

........2b - If session variable is set then check the value and according to value display the different content you want to show to user like: "data not found message and search panel"

注意:在自定义错误页面的编码中你需要再次设置404 header,并且你还可以根据session变量发送不同的错误代码上述技巧的价值。