Script for 404 random page found

Some people have been wondering how I did the random selection of 404 page not found messages that are used on this web site.

So, here is the nice little PHP script that I use here to redirect randomly to one of the right pages:

	$address_list = array(
/*		"/404/404-07.php",	*/
	$randomtopic = rand(0,count($address_list));
	header('Location: '.$address_list[$randomtopic]);

You can re-use it on your own web site if you want to. It’s free.



, ,




2 responses to “Script for 404 random page found”

  1. Xtian Avatar

    A couple of remarks:

    When you redirect an error page, don’t forget to set the error status in your page, otherwise the error is lost. You can add:
    header(“Status: 404 Not Found”);

    Why don’t you look for all files in the 404 directory and choose one of them?
    $dir = “somewhere/404/”;
    if (is_dir($dir)) {
    if ($dth = opendir($dir)) {
    while (($file = readdir($dth)) !== false) {
    if (preg_match(‘/^404-.*\.php$/’, $file) == 1) {
    $address_list[] = $dir . $file;

  2. Yves Roumazeilles Avatar

    That’s right, but did you notice that some (one) of the pages are not displayed? Actually, there are authorizations and copyright issues that limited the number of pages I accepted to go to the user. And this was changing pretty often (there is still one page not displayed because of this).

    Actually, your solution is nicer for sure.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.