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

657 views Asked by At

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.

1

There are 1 answers

0
Adarsh Rajput On BEST ANSWER

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

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.