Forum Moderators: phranque

Message Too Old, No Replies

.htaccess custom 404 page not work with php header(404)?

         

iProgram

4:21 am on Nov 13, 2004 (gmt 0)

10+ Year Member



Here is my .htacess file

ErrorDocument 404 /404.html

It works.

In some php file, I need to send a 404 not found header, using the following code:


<?php
header("HTTP/1.0 404 Not Found");
?>

But this will pass the .htaccess file, I could not see the /404.html file opens. How to solve this problem?

jdMorgan

6:58 am on Nov 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



.htaccess runs during the URL-to-filename API phase of Apache,after an HTTP request is received, and before the content-handler is invoked. Once you have invoked php, it is too late for .htaccess to do anything. Therefore, you should call your php 404 error function directly from your script when you detect that you cannot serve content. The script must output all required headers to form a valid 404-Not Found error response page.

You will end up with a 404 "page" that will be invoked by Apache if it cannot find content to serve based on a file-exists or file-does-not-exist test. Then your script wil contain a 404 error reporting function of its own that does the same thing based on a database-record-exists test. Both of these php scripts can call or include the same shared function, but as "pages" they will be separate things.

I hope that is explained clearly enough... The problem is not with Apache, but rather with your expectations.

Jim