,

PHP:How to do 404 not found correctly

<?php
header("HTTP/1.0 404 Not Found");
header("Status: 404 Not Found");
$_SERVER['REDIRECT_STATUS'] = 404;
echo $error_page;# HTML with the page you want to display
exit;
?>

Steps to perform 404

  1. Set up 2 headers and a server variable to cover all bases
  2. echo (or print) the error page you want. This is a good practice if the page in question isn't too big and when you want to display a personalised message for each mistaken URL that hits your server
  3. Exit! Important to avoid errors on the error page.


.htaccess - 404

Yes, you can also use the .htaccess file to do display your error pages, althought this won't help much with printing a personalised error message for each page that isn't found.

RewriteEngine On
RewriteOptions Inherit

ErrorDocument 404 /error.php