PHPBuilder - PHP handler for Apache mod_spel



RSS Twitter
Snippets Http

PHP handler for Apache mod_spel

by: Richard Vasquez
|
February 18, 2001

Version: 1.0

Type: Sample Code (HOWTO)

Category: HTTP

License: GNU General Public License

Description: This piece of code will interact with Apache's mod_spel module and return results that lets you customize the output.



The Apache web server module mod_spel is a life saver in ensuring your site is accessible. The module catches single letter typos, and if there's only one possible match to the typo, it automatically directs you to the correct page. 

However, if you have more than one possibility, then mod_speling generates a plain page with a list of possible URLs. It's functional, but if you like to maintain a specific look/feel on all your pages, even the error and warning pages, you might want to look at this code for a way to do it in PHP. 

The first thing you have to do is modify your Apache configuration file so that an HTTP warning level of 300 is handled by this script. An example of this would be: 

ErrorDocument 300 /errpages/300.php 

In the file named 300.php, you would add the code that's after this, and use the function mod_speling_urls() to retrieve an array of URLs, plus the reason that document met the mod_speling criteria. 

The code that follows just presents an ordered list with the URLs and the reason they matched. You can use this as a stepping stone to creating your own mod_speling output. 

See it in action by using this typo'd URL:
http://www.chaos.org/knowledge/photoshop/photoshop0.html

<?php
  /*
   * mod_speling_urls()
   *
   * Usage:
   *
   * $url_list = mod_speling_urls();
   *
   * url_list will be an array with the following
   * indices:
   *    [###]["url"]     | ### is a number from 0
   *                     | to whatever.  Find the
   *    [###]["reason"]  | maximum by
   *                     | count($url_list)
   *
   * After retrieving the array, you can create
   * whatever output you see fit.
   */
  function mod_speling_urls()
  {
    global $REDIRECT_VARIANTS;

    $idx[0]="url";
    $idx[1]="reason";

    $tmp_array = explode('","', stripslashes($REDIRECT_VARIANTS));

    for($loop_1 = 0; $loop_1 < count($tmp_array); $loop_1++)
    {
      $tmp_parts = explode(";", $tmp_array[$loop_1]);

        for($loop_2 = 0; $loop_2 < 2; $loop_2++)
        {
          // A regex that would do this better would
          // be greatly appreciated.
          $clean = $tmp_parts[$loop_2];
          $clean = str_replace('"', "", $clean);
          $clean = str_replace(",", "", $clean);
          if($loop_2 == 0)
          {
            $list[$loop_1]["url"]=$clean;
          }
          else
          {
            $list[$loop_1]["reason"]=$clean;
          }
       }
    }
    return $list;
  }

  $url_list = mod_speling_urls();

  // Place whatever PHP code and/or HTML is needed
  // for the top of your page here.
?>
<OL>
<?php
  for($loop_1 = 0; $loop_1 < count($url_list); $loop_1++)
  {
    $tmp_url=$url_list[$loop_1]["url"];
    $tmp_reason=$url_list[$loop_1]["reason"];
    echo '<LI><A HREF="' . $tmp_url . ">$tmp_url</A> ($tmp_reason)\n";
  }
?>
</OL>
<?php
  // Place whatever PHP code and/or HTML is needed
  // for the bottom of your page here.
?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Richard Vasquez

Comment:



Comment:

(Maximum characters: 1200). You have characters left.