Version 3

    It's nice to have URL's that look something like this:

     

      http://my.server.net/article/0000/

     

    ...rather than like this:

     

      http://my.server.net/blog.py?article=0000

     

    It's more aesthetic, and it means you can rename your blog script, or use perl instead of Python.

     

    But it's hard to do that, because you have to rework the directory structure.

     

    This page tells how to handle the directories yourself, taking that work over from Python.

     

    The best news is: You don't even have to touch ModRewrite!

     

    The basic idea is:

    1. Do an InternalRedirect for all traffic on a VirtualHost to your script.

    2. In the script, ask Apache for the previous RequestObject's URI.

     

    This page assumes mod_python. If you're not using mod_python, read what you can, and it

    may give you hints about how to do this with your own configuration.

     

    == Redirect Traffic ==

     

    Assuming you have ModPython setup, add the following lines to the VirtualHost entry:

      PythonPath "sys.path+['/var/www/path/to/your/script/directory/']"
      AddHandler python-program .py
      PythonPostReadRequestHandler allrqst
      PythonDebug on
    

    (I don't know that all of this is necessary. Someone more expert than me can correct this.)

     

    The most important line here is "PythonPostReadRequestHandler." That means that, when a

    request comes in, before anything is done with it, it will be sent to the "allrqst"

    module. The "allrqst" module will be in /var/www/path/to/your/script/directory/.

     

    Make the file "allrqst.py" in that directory, and have it read:

     

      from mod_python import apache
    
      def postreadrequesthandler(req):
        if req.uri != "/blog.py":
          req.internal_redirect( "/blog.py" )
        return apache.OK
    

     

    That's it! That will redirect ALL TRAFFIC to your script, /blog.py, regardless of URI.

     

    But we're not done yet- we still want to have access to that old URI.

     

    == Ask Apache for the Previous URI ==

     

    First, you need to tell Apache how to find blog.py. In the VirtualHostDirective, add:

     

      <Directory "/var/www/path/to/your/script/directory/">
        AddHandler python-program .py
        PythonHandler blog
        PythonDebug on
      </Directory>
    

     

    Then have the code for "blog.py" will print the old URI.

     

      from mod_python import apache

     

      def handler(req):

        req.write( "Old URI: " + req.prev.uri )

        return apache.OK

     

    == Summary ==

     

    So, the basic idea is:

    1. Do an InternalRedirect for all traffic on a VirtualHost to your script.

    2. In the script, ask Apache for the previous RequestObject's URI.

     

    == Questions ==

     

    • Can you do the InternalRedirect without dropping into mod_python, mod_perl, or anything else? Can use just use a normal Configuration Directive?