404 Header returns 200 OK

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jchannon
Forum Newbie
Posts: 12
Joined: Fri Aug 03, 2007 1:55 pm

404 Header returns 200 OK

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: 404 Header returns 200 OK

Post by VladSun »

Code: Select all

if (!$pageFound)
{
    header("HTTP/1.0 404 Not Found");
    exit();
}
 
There are 10 types of people in this world, those who understand binary and those who don't
jchannon
Forum Newbie
Posts: 12
Joined: Fri Aug 03, 2007 1:55 pm

Re: 404 Header returns 200 OK

Post by jchannon »

That makes no difference.

I have tried all combinations of exit and die.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: 404 Header returns 200 OK

Post by VladSun »

Are you sure this code block gets executed?
There are 10 types of people in this world, those who understand binary and those who don't
jchannon
Forum Newbie
Posts: 12
Joined: Fri Aug 03, 2007 1:55 pm

Re: 404 Header returns 200 OK

Post by jchannon »

Yes because I get a blank white screen in Firefox which I assume is the header kicking in.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: 404 Header returns 200 OK

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
jchannon
Forum Newbie
Posts: 12
Joined: Fri Aug 03, 2007 1:55 pm

Re: 404 Header returns 200 OK

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: 404 Header returns 200 OK

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
jchannon
Forum Newbie
Posts: 12
Joined: Fri Aug 03, 2007 1:55 pm

Re: 404 Header returns 200 OK

Post by jchannon »

Yes I see that message!
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: 404 Header returns 200 OK

Post by VladSun »

Are there any "headers already sent" messages in your error log?
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply