Typical scenario:

  1. DB products are displaied in page http://...?item_id=467
  2. User eventually removes the product
  3. Google or perhaps a user tries to access http://...?item_id=467
  4. PHP diggs into DB and sees products doesn't exist any longer, now PHP must tell Google/user that item isn't existing using a 404 header and page.

Based on this answer I undertstood there's not a way to redirect to 404 Apache page via PHP unless of course delivering in code the 404 header + reading through and delivering lower to client all of the items in your default 404 page.

The probelm: I curently have an Apache parsed custom 404.shtml page, so obvioulsy I must simply employ that page. But basically read an shtml page via PHP it will not be parsed by Apache any longer.

What exactly would you suggest me to complete?

Can there be maybe some trick I possibly could use palying with htaccess too?


Hmm. Two ideas spring to mind:

  • Redirect towards the 404 page using header("Location:...") - this isn't standards-compliant behavior though. I'd use that only like a last hay

  • Fetch and output the Apache-parsed SHTML file using file_get_contents("http://mydomain.com/404.shtml"); - also not really optimal just because a request is built to the net server but, I believe, acceptable generally.

I doubt there's anything that you can do in .htaccess since the PHP script runs after any rewrite rules happen to be parsed.

If you work with apache mod_php, use virtual('/404.shtml'); to show the parsed shtml page for your user.

I had been trying to get this done identical factor yesterday.

Does Pekka's file_get_contents/include create a 404 status header being sent? Possibly you must do this before such as the custom error page?

header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");

You can look at using this Firefox extension.