404 Page not found with PHP
Posted October 29th, 2008 in PHP
A PHP script will normally return a 200 HTTP code to the browser which means the page has been successfully retrieved but it's possible to also return other status codes when redirecting (e.g. doing a permanent redirect) or a 404 to indicate a page has not been found. This post looks at how to do a 404 page not found error with PHP.
It's really simple:
header("HTTP/1.0 404 Not Found");
You can then send a regular web page to the browser which tells the user that the page cannot be found etc etc and maybe have useful links to popular pages, a search function or similar.
By sending a 404 header (instead of a regular 200 header) along with content you are telling in particular search engine robots that the page doesn't exist or no longer exists. This should usually mean they stop requesting the page in question.
Related posts:
- Setting 503 Service Temporarily Unavailable headers with Apache .htaccess (Sunday, March 14th 2010)
- 410 "Gone" with PHP (Thursday, September 3rd 2009)
- 303 redirect with PHP (Thursday, July 31st 2008)
- Image headers with PHP (Tuesday, July 22nd 2008)
- 301 redirect with PHP (Saturday, March 15th 2008)

Comments
blog comments powered by Disqus