Handling 404 Errors with PHP

30 Oct 2004

The PHP site does some nice trickery with 404 errors (plus 401 and 403). This is what accomodates short URLs like http://www.php.net/security. Want to do something similar on your own site? I see people asking how to do this all the time, even though the entire site is open source. Just have a look at the error.php script and write something similar for your own site. Use Apache's ErrorDocument directive to have your own error.php script handle any error code you want:

  1. ErrorDocument 401 /error.php
  2. ErrorDocument 403 /error.php
  3. ErrorDocument 404 /error.php

If you have a more flexible error script that people can implement with less hacking, feel free to let me know (or just leave a comment).