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:
- 410 "Gone" with PHP (Thursday, September 3rd 2009)
- Apache does not log HTTP status set with PHP header when using mod_rewrite in .htaccess files (Sunday, April 5th 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)
Share or Bookmark
Share or Bookmark this page using the following services. You will need to have an account with the selected service in order to post links or bookmark this page.
Subscribe or Follow
Subscribe via RSS or email, or follow me on Facebook or Twitter below. The RSS icon takes you through to Feedburner where you can select the service or application to use.

