Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Setting the 404 header info

In what file should I add it? In /404.php?



8:09 pm on Apr 11, 2010 (gmt 0)

5+ Year Member

Hello. This was discussed before but most threads were too old to reply there.

I want to set the 404 header info when a page was not found.
I have a 404 page on my server to show when a page was not found. The page is /404.php, is this the file where I should add the header info?

header("HTTP/1.0 404 Not Found");
header("Status: 404 Not Found");
<h1>Sorry, page not found</h1>

Is this the correct location to add this?


9:01 pm on Apr 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Yes it is, top of the page, before any html. Not even blank space


9:49 pm on Apr 11, 2010 (gmt 0)

5+ Year Member

And do you know if setting the header info like that works with specifying the 404 page in .htaccess by this mean?

ErrorDocument 404 http://example.com/404.php

Doesn't Apache already set the proper info in file header if I have this in htaccess?


11:01 am on Apr 17, 2010 (gmt 0)

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

ErrorDocument 404 http://example.com/404.php

Actually, the above is an 'external' location for the file (even though I'm guessing it's on your website) so Apache will correctly send a '302 Found' (AKA Undefined) redirect status code, so in this case you will have to set the 404 header on the error page... (This is a redirect.)

ErrorDocument 404 /404.php

Is an 'internal' location, so rather than sending the browser there, Apache 'gets' the page and serves the information from it, so a 404 Not Found will be served to the visitor... (This is a rewrite.)

IMO the safest thing to do is use the server relative path (starts with a / as above) and go ahead and set the 404 on the PHP too... It's a bit of double coverage, and as long as you have it correct then it doesn't hurt. I actually have a set of custom error page 'starting points' that contain certain things I use on them, such as the PHP status code header for the error, and noindex,nofollow,noarchive robots meta tags, a 'the URL: http://www.example.com/url-here.html has been disconnected or is no longer in service' message (actually, I think that one's my answering machine, but I'm sure you get the point, and I think that one's going on my new 404 pages, because I like it. LOL.), a link to the home page for sure (and usually a few 'key' pages I think people might like to visit) etc. Then I just put a different template on them for different sites.

Also, it's usually recommended to always check error page headers, which can be done with a FireFox Plugin (Live headers, I think) or (what I usually use since I've been using it for years) is the server header check here in your control panel... Go to the control panel link at the top of the page, give it a click, turn left, pause to look at the neat little picture above the calendar for a second, look down at the bottom of the links on the left, click the second one up where it says 'server headers', copy and paste a broken link from your site in the box, hit 'submit' and you'll know what code the bots are getting for that page... Sometimes it's a good idea to do on pages you think are supposed to work too, just to see what's being sent. :)


11:50 am on Apr 17, 2010 (gmt 0)

5+ Year Member

Thanks for the tips, 404 path relative to site root looks better, I was not sure how that is made, so now that it is rewrite it keeps the URL in address bar and it says it was not found.
I use liveheaders but not so advanced, I will look more into its features.

Featured Threads

Hot Threads This Week

Hot Threads This Month