[mod_python] SetHandler trick to have pretty url's

Graham Dumpleton grahamd at dscpl.com.au
Tue Jan 10 04:14:55 EST 2006


On 10/01/2006, at 5:52 PM, courtney ludwin wrote:

> I was looking for a way to pretty up my mod_python
> url's the other day and I figured out a trick that
> works w/ the 'FilesMatch' directive in httpd.conf, I
> figure I'd share with the list....
>
>  <FilesMatch "(^[^\.]*$|^[^\?]*[\?]+[^$]+$)">
>     SetHandler python-program
>     PythonHandler common.dispatch.dispatcher
>     PythonDebug On
>   </FilesMatch>
>
> This match expression says anything without a file
> extension should be handled by dispatcher.py otherwise
> apache should handle it normally.  I wrote the
> dispatcher use a config file to map url's to
> mod_python handlers.
>
> Followed up a quick example in my blog:
> http://2centspoorer.blogspot.com/2006/01/creating-your-own-modpython- 
> request.html
>
> Hope you find it useful.

For simple cases, your handler could simply include the following at
the start:

   stub, extn = os.path.splitext(req.filename)
   if not extn:
     return apache.DECLINED

Returning apache.DECLINED will trigger Apache to perform its normal
handling of static files. Thus, accessing any static file such as
an image will still result in the image being returned.

If you wanted to mix PHP in the same directory, you have to be a
bit more tricky and disable mod_python in the Apache configuration
for the .php extension if using this trick. Ie.,

   SetHandler python-program
   PythonHandler common.dispatch.dispatcher
   PythonDebug On

   <Files *.php>
   SetHandler None
   </Files>

This latter approach means everything goes to Apache but you disable
it for extensions which should be handled by Apache. This works the
opposite way of what you are doing. You could even say:

   SetHandler python-program
   PythonHandler common.dispatch.dispatcher
   PythonDebug On

   <Files *.*>
   SetHandler None
   </Files>

This does away with your complicated FilesMatch pattern altogether and
the change to the handler isn't needed either.

Graham



More information about the Mod_python mailing list