4

TIP : How to get Apache Location directive to match trailing slashes

 3 years ago
source link: http://www.mikechambers.com/blog/2008/05/01/tip-how-to-get-apache-location-directive-to-match-trailing-slashes/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

TIP : How to get Apache Location directive to match trailing slashes

Thursday, May 1, 2008

I am working on deploying a django app online, and thus need to use the Apache Location directive to point to my django app.

This is pretty straight forward:

<Location "/foo/">
        SetHandler python-program
        PythonPath "['/path/to/djangositedir/'] + sys.path"
        PythonHandler django.core.handlers.modpython
        SetEnv DJANGO_SETTINGS_MODULE settings
        PythonDebug On
</Location>

Note: You will normally add / edit this in the httpd.conf file for your apache installation.

Basically, you just map “/foo/” to the python handler. Anytime someone goes to a URL that begins with ““/foo/”” on your site, it will be handled by Python and your django app.

However, if someone leaves of the trailing slash like so “/foo” then the location doesnt match and the server will return a 404.

Luckily, this is fairly easy to fix, and I wanted to post it here for future reference. Just use a regular expression to match either “/foo” or “/foo/”. Here is the updated directive:

<Location ~ "/foo(/|$)">
        ...
</Location></pre>

Note that the “~” is important as it enabled regular expression support. Basically, this says match anything that begins with “/foo” and either has a trailing “/” or ends “$”.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK