Configuration: Apache + mod_python

Graham.Dumpleton at gmail.com Graham.Dumpleton at gmail.com
Thu Mar 8 06:18:19 EST 2007


On Mar 8, 9:50 pm, "Danilo" <dbrab... at gmail.com> wrote:
> Hi there,
>
> is it possible to create a rewrite rule to send every server-request
> to the directory /py? But only if the file does not exists on the
> server.
>
> This is my mod_python section of the apache config-file.
>
> <Location "/py">
>         SetHandler python-program
>         PythonHandler django.core.handlers.modpython
>         PythonPath "['/var/www/mydomain.com/htdocs/py'] + sys.path"
>         SetEnv DJANGO_SETTINGS_MODULE myapp.settings
>         PythonDebug Off
> </Location>

For the more general case of where a HTTP 404 error would otherwise be
returned, indicating that a resource could not be found, as opposed to
an actual physical file, you can just use:

  ErrorDocument 404 /py

This would be simpler than using mod_rewrite. I can't remember though
whether the handler when triggered in this case can change the
response status to something other than 404.

You could use mod_rewrite if you really must, but not sure how it
would interact with virtual resources managed by some handler where no
actual file exists. To be practical you would probably want to
restrict the scope of mod_rewrite to specific contexts.

Quoting an example from very good book "The Definitive Guide to Apache
mod_rewrite", you can do something similar to:

  RewriteEngine On
  # If its not here ...
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  # Look here instead ...
  RewriteRule ^/images/(.*) /pics/$1 [PT]

In this case it is causing lookups for images to be made in two
places, but your case wouldn't be much different.

Graham




More information about the Python-list mailing list