2

I am using PHP for my project and I have .htaccess file which has code below;

ErrorDoccument 404 /error-handler.php

This code is used to send or use error-handler.php file for every 404 error. Everything till here is working. But I want a file named call-404.php which when opened from browser should show 404 error and it must be handled by .htaccess file above. I tried using;

http_response_code(404);

also,

header("HTTP/1.1 404 Not Found");

All the above code does says HTTP ERROR FILE NOT FOUND but the 404 at this condition does not does as per .htaccess file. It just displays browser defined 404 error page. What is the solution for it?

And This question is not simply 404 redirection using .htaccess or simply use of http_response_code(404);. This question shows and wants solution of inability of .htaccess redirection on response to http_response_code(404).

Niyoj oli
  • 342
  • 3
  • 15

1 Answers1

-1

The whole point of ErrorDoccument 404 /error-handler.php is that if NO resource is found (be it image, php script or whatever) then apache will use that directive and head to that 404 page. If however you have reached one of your own scripts, then a resource was found and therefore that .htaccess rule will not be triggered.

But you're almost there. Just header and then call the script!

<?php

header("HTTP/1.1 404 Not Found");
echo file_get_contents('/path/to/your/404page.php');

I presume that the 404 page has nothing dynamic and is just plain HTML?

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39