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