PERL: Intelligent redirect

Roy Tennant rtennant at library.berkeley.edu
Tue Mar 10 19:19:24 EST 1998


A number of Perl wizards came to my aid, and had some really good
suggestions. Both Jamie McCarthy and Thomas Dowling brought up the Apache
module, which thankfully gave me more ammunition with which to persuade
the powers that be to install Apache. But Daryl Friesen gets the prize for
coming up with the solution I was looking for at the moment, and so this
is what I have so far. My next step is to create a subroutine to do the
check and swap, while taking values from an array. Many thanks to all who
responded,
Roy

#!/usr/local/bin/perl
#
# Name: notfound.cgi
# Author: Roy Tennant, rtennant at library.berkeley.edu, with assistance
#         from my Web4Lib colleagues, most notably Darryl Friesen
#         <friesend at moondog.usask.ca>
# Purpose: To allow me to redirect users more intelligently based on the
#          idea that most "file not found" errors are the result of simple
#          typing errors.
# How it's called: When a request would create a "404 not found" error,
#          the client is bounced to this program, which performs some
#          checks. If an appropriate site cannot be found, the user gets
#          a static "not found" page that provides ways in which they can
#          locate where they want to go.
#

# Grab what they entered

@temp = split(/=/,$ENV{'QUERY_STRING'});
$_ = $temp[1];

if (/govdata/i) {
  s/govdata/GovData/i;

  # Check what comes after GovData to see if the file/directory is valid
  $_ =~ /GovData(.*)/;
  $whatever = $1;       # This is what followed 'GovData'

    if (-e "/data/_a/webdata/GovData/$whatever") {
      print "Location: $_\n\n";
    } else {
      print "Location: /errors/notfound.html\n\n";
    }

  } else {  # When all else fails...
  print "Location: /errors/notfound.html\n\n";
}




More information about the Web4lib mailing list