About the Author

Chris Shiflett

Chris Shiflett is an author and speaker who leads the web application security practice at OmniTI.


Handling 404 Errors with PHP

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:

ErrorDocument 401 /error.php
ErrorDocument 403 /error.php
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).

About This Post

Handling 404 Errors with PHP was posted on Sat, 30 Oct 2004 at 07:01:08 GMT.

4 Comments

1. Ryan's GravatarRyan said:

If looking for something to do with a 404 page, take a look at this article[http://www.alistapart.com/articles/perfect404/].

Sat, 30 Oct 2004 at 15:45:08 GMT Link


2. Chris's GravatarChris said:

There is a problem with this method...

Each page hit still registers as a 404 in apache error logs.

Looking at stats reports (eg webalizer, awstats), you see a lot of 'missing' pages. Depending on how much you interpret & use stats packages, this could cause a problem.

Mon, 01 Nov 2004 at 02:15:08 GMT Link


3. David's GravatarDavid said:

I wrote an article about this for ONLamp.com a little while ago. It's available here:

--> http://www.onlamp.com/pub/a/onlamp/2003/02/13/davidsklar.html

Mon, 01 Nov 2004 at 18:55:24 GMT Link


4. Chris Shiflett's GravatarChris Shiflett said:

Well done, Dave. That's way better. :-)

Mon, 01 Nov 2004 at 19:37:05 GMT Link


Post A Comment

Personal Details and Comment

Style Guide

Line breaks are converted to paragraphs. Also use:

  • <a href="" title="">text</a>1
  • <em>text</em>
  • <blockquote><p>text</p></blockquote>
  • <code>2  <?php  if ($foo) {      $foo = TRUE;  }  ?></code>
  1. Note: <code> can be used inline (e.g. in paragraphs) or in a block as shown. Include whitespace and newlines in blocks.

Please enter Chris (my first name) below. This is a primitive spam prevention technique, and I apologize for the inconvenience.

Preview and Submit

Upcoming Talks

O'Reilly Open Source Convention

21 - 25 Jul 2008

At Oregon Convention Center, Portland, Oregon.

ZendCon

15 - 18 Sep 2008

In Santa Clara, California.

PHP Appalachia

11 - 14 Oct 2008

At Big Bear Lodge, Gatlinburg, Tennessee.

New Comments

Amir wrote:

Hi chris! Please check this and guide me: http://forums.devnetwork.net/viewtopic.php?f=34&t=8...

Posted in
Nathan Bentley wrote:

Hi Chris, A great tutorial, which should help a lot of people! We implemented something simil...

Posted in
Daniel S wrote:

Just recently I sold my 1.gen Macbook(core duo version). And to be honest, I don't miss it for on...

Posted in Top X List of Mac OS X Annoyances
Buke Beyond wrote:

I agree it is ridiculous that php is doing this. I am using php for generating commands for othe...

Posted in PHP Stripping Newlines
Davis Ford wrote:

I agree, although I have a list of many more annoyances. However, rather than complain about the...

Posted in Top X List of Mac OS X Annoyances

Browse Comments