Writing a MODULE_LAST
 
    How to write a MODULE_LAST ?
Name

How to write a MODULE_LAST.

Versions

Caudium 1.0, 1.2, 1.3

Synopsis

A MODULE_LAST is handed control whenever no other module was available to fulfill the request. In most cases, this is a file not found error but can happen in a few other instances.

Documentation

The premise of the attached MODULE_LAST is to do a redirect to a URL specified in the config interface, or to the / filesystem of the existing Host.

The module doesn't check to see if it has redirected the person already, so if you generate a 404 error on the root filesystem, it will do an infinite loop until either the browser decides not to continue or the surfer is convinced that there is nothing more to see.

In the Config Interface, you can click on the Status and Debug section of the module definition and see how many people were redirected. This is accomplished with a global variable called count that is defined in the module outside of any of the subroutines, and count is incremented in the last_resort section of the module.

The reason this module was originally written was to send 404 traffic to a separate site and the old 404 method within Caudium didn't allow a redirect to be parsed. The new Error Theme files may handle this by parsing the file being output, but I haven't tested it.

Source
/*
 * Caudium - An extensible World Wide Web server
 * Copyright © 2001-2002 The Caudium Group
 * Copyright © 2001 Davies, Inc
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */
/*
 * $Id: 404.pike.txt,v 1.2 2002/10/04 17:35:58 grendel Exp $
 */
/*
 * See http://www.daviesinc.com/modules/ for more informations.
 */

#include <module.h>

inherit "module";
inherit "caudiumlib";

// #define PATHINFO_DEBUG

//! module: 404 redirector
//!  Error 404 redirector: simple MODULE_LAST that redirect to some url
//!  when no modules can handle the request.<br />Based on Chris Davies 
//!  <a href="http://www.daviesinc.com/modules/">module.</a>
//! type: MODULE_LAST
//! inherits: module
//! inherits: caudiumlib
//! cvs_version: $Id: 404.pike.txt,v 1.2 2002/10/04 17:35:58 grendel Exp $

constant module_type = MODULE_LAST;
constant module_name = "404 redirector";
constant module_doc  = "Error 404 redirector: simple MODULE_LAST that redirect "
                       "to some url when no modules can handle the request. "
                       "<br/>Based on Chris Davies <a href=\"" 
                       "http://www.daviesinc.com/modules/\">module.</a>";
constant module_unique = 1;
constant cvs_version = "$Id: 404.pike.txt,v 1.2 2002/10/04 17:35:58 grendel Exp $";
constant thread_safe = 1;

void create() {
  defvar ("url", "http://www.yahoo.com", "URL",
          TYPE_STRING,
          "URL to redirect to",
          );
}

mapping|int last_resort(object id)
{ 
  return http_redirect(QUERY(url), id);
}

/* START AUTOGENERATED DEFVAR DOCS */

//! defvar: url
//! URL to redirect to
//!  type: TYPE_STRING
//!  name: URL
//

  
Download the source
Get the Module also available on Caudium 1.2 and 1.3 source tree.
 
HTML OK CSS