Page 1 of 1

404 Header returns 200 OK

Posted: Fri Jan 23, 2009 7:03 am
by jchannon
Hi,

I have spent hours trying to get all this to work with no luck so was after some help.

I have a content based site where I check if the pagename exists and if it doesn't I do the following:

Code: Select all

 
if (!$pageFound)
{
    header("HTTP/1.0 404 Not Found");
}
 
However if I check the response headers I get 200 OK.

That is the first issue.

At the moment I have rewriting turned on so at the moment a url of http://www.domain.com/testpage.html gets rewritten to http://www.domain.com?index.php?page=testpage

If they type something in like http://www.domain.com/pagenotexist.html it will run the above code and return a 404 header.

How can I handle these scenarios?

Thanks

Re: 404 Header returns 200 OK

Posted: Fri Jan 23, 2009 7:07 am
by VladSun

Code: Select all

if (!$pageFound)
{
    header("HTTP/1.0 404 Not Found");
    exit();
}
 

Re: 404 Header returns 200 OK

Posted: Fri Jan 23, 2009 7:15 am
by jchannon
That makes no difference.

I have tried all combinations of exit and die.

Re: 404 Header returns 200 OK

Posted: Fri Jan 23, 2009 7:21 am
by VladSun
Are you sure this code block gets executed?

Re: 404 Header returns 200 OK

Posted: Fri Jan 23, 2009 7:31 am
by jchannon
Yes because I get a blank white screen in Firefox which I assume is the header kicking in.

Re: 404 Header returns 200 OK

Posted: Fri Jan 23, 2009 7:32 am
by VladSun

Code: Select all

if (!$pageFound)
{
    error_log()
    header("HTTP/1.0 404 Not Found");
    exit();
}
and check if '404 header sent.' appears in your error logs.

Re: 404 Header returns 200 OK

Posted: Fri Jan 23, 2009 7:46 am
by jchannon
Nothing appears in the error_log file regarding a 404, you have to supply a message to that function and that message appears.

In the log file for today there is reference to the 404 errors

Re: 404 Header returns 200 OK

Posted: Fri Jan 23, 2009 8:09 am
by VladSun
Yeah, cut&paste error :)

Code: Select all

if (!$pageFound)
{
    error_log( '404 header sent.');
    header("HTTP/1.0 404 Not Found");
    exit();
}
So ... do you see the exact "404 header sent." message in your error logs? Any other messages regrading 404 are out of interest.

Re: 404 Header returns 200 OK

Posted: Fri Jan 23, 2009 8:16 am
by jchannon
Yes I see that message!

Re: 404 Header returns 200 OK

Posted: Fri Jan 23, 2009 9:45 am
by VladSun
Are there any "headers already sent" messages in your error log?