ModPerl.pl: ...where persistent camels roam...
 

    Sections:  
How-to - ModPerl scripts

This document provides a basic "404 Not found" error handler Perl script.

§ Dynamic content

Although we will begin with "index.pl" (which is the dynamic pure-Perl equivilant to the usual "index.html"), you can create your scripts with any base filename.

Create the following file, which will generate the traditional "Hello, world" output:

  • /internet/com/example/www/index.pl

Then, add the following content to that newly created Perl script file:

use strict;
use warnings;

# -----------------------------------------------------------------------------
# Initialization.
# -----------------------------------------------------------------------------
my $r = shift; # --- This is the Apache2::RequestRec object
my $f = new www_Example($r);

# -----------------------------------------------------------------------------
# Headers.
# -----------------------------------------------------------------------------
$f->send_header(title => 'Hello, world');

# -----------------------------------------------------------------------------
# Page content.
# -----------------------------------------------------------------------------
$r->print('
<p>
  <font size=+2>Hello, world!</font>
<p align=justify>
  Is this your first dynamic ModPerl script?  If you\'re seeing only this line
  of text and the heading above it, then you succeeded and congratulations are
  in order -- please share your success story with us:&nbsp;
  <a href=http://www.modperl.pl/ target=_blank>http://www.modperl.pl/</a>
');

# -----------------------------------------------------------------------------
# Footers.
# -----------------------------------------------------------------------------
$f->send_footer;

We don't need "use www_Example;" to load the Perl Module you just created (see the previous page) because we used the "PerlRequire" directive in the virtual host configuration file to pre-load it (and pre-compile it) when starting the Apache HTTPd server daemon.

Did you notice that the amount of HTML code included in the "page content" section of this script is minimal?  The $f->send_header and $f->send_footer subroutines generate that for you, and the result is a "404 Not found" page that fits in with the consistency of the rest of the web site, and giving it that important "first-class styled professional appearance."

§ 404 Not found handler

When a page, image, etc., is not found (or when another script generates a 404_NOT_FOUND response), the output of this script is sent instead of the default response provided by Apache HTTPd server.

There is more than one way to implement a 404 handler, but this one is elegant because it doesn't change the URI.  Some examples of what this script can be extended to do include:

  • examine the requested URI that failed
  • examine any arguments (CGI parameters that use the GET and/or POST methods)
  • send redirection HTTP headers (e.g., to correct a mis-spelling)
  • present a user-friendly help page with a dynamically generated site map
  • present a search dialogue possibly containing some pre-set keywords based on the failed request
  • cross-reference a database to determine what this user might be trying to find and present some helpful links
  • notify the webmaster on the server-side via eMail that an error occurred after filtering out certain "agents" (spiders, hacking scripts, etc.)
  • provide the user with a "please tell us what you were searching for" dialogue box

Create the following file, which was referenced in your virtual host configuration file by the "ErrorDocument" directive; this will serve as your site-wide "404 Not found" handler:

  • /internet/com/example/www/404.pl

Then, add the following content to that newly created Perl script file:

use strict;
use warnings;

# -----------------------------------------------------------------------------
# Initialization.
# -----------------------------------------------------------------------------
my $r = shift; # --- This is the Apache2::RequestRec object
my $f = new www_Example($r);

# -----------------------------------------------------------------------------
# Headers.
# -----------------------------------------------------------------------------
$f->send_header(title => '404 Not found');

# -----------------------------------------------------------------------------
# Page content.
# -----------------------------------------------------------------------------
$r->print('
<p>
  <font size=+2>404 Not found</font>
<p align=justify>
  The requested URI was not found on this example server.
');

# -----------------------------------------------------------------------------
# Footers.
# -----------------------------------------------------------------------------
$f->send_footer;

Since Apache HTTPd already set the response to 404_NOT_FOUND, we don't need to specify this at the end of the script.  If we were changing the response to OK, or re-directing to a different URI, then we would need to override this, but for a typical "404 Not found" page we want to keep this error response in tact so that web spiders and other automated processes can correctly identify this as an error page.

Previous | Index

 
Home | Contact us
Copyright © Inter-Corporate Computer & Network Services, Inc.  All rights reserved.
All trademarks are the property of their respective owners.