简体   繁体   中英

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

Lets understand the scenario:

:- Using PHP for a web application on IIS 8.5

:- A custom error page is set for 404 with following web-config snippt:

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

:- This is working for non-existing page and IIS also return this 404.php if I set header to 404 (via header('HTTP/1.0 404 Not Found', true, 404); die; ) in any PHP page.

Question:

Now, I wan to send 404 http response code in some php-file on any specific condition, lets say if database returns 0 rows. I can do that by setting header as above but that will return the custom error page also. Is there any way to send 404 code and tell IIS to send response as it is, without sending custom error page along with it?

Issues:

:- If I remove custom error page and put the content between header() and die() then it will meet my requirement but IIS will fail to serve custom error on pages which actually doesn't exist.

:- I am using URL rewrite so that cannot use multiple custom error pages based on location tag and don't want to redirect to any suggested different URLs.

As I have one tricky solution/idea, I want to share that also. But if anybody have any better solution, I welcome and thanks to them.

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; 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 "

Note: In custom error page's coding you again need to set 404 header, and ALSO you can send different error code according to session variable's value in above trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM