Configuring django application to run at a subdirectory — הכל בראש
מדעי המוח, תכנות, חברה, כלכלה, זכויות יוצרים וכל דבר אחר שעובר לי בראש

Configuring django application to run at a subdirectory

תגיות:‏ , , , , ,

אזהרה: פוסט טכני לאללה.

sounds easy? well it's not, if your shared web hosting1 uses mod_rewrite to edit out a part of the request. this was my problem:

user sends HTTP get to ofri.webfactional.com/django/.*

the main hosting apache grabs this, and (my guess) uses mod_rewrite to send the request to my own instance of apache, but now the request looks like it was sent to ofri.webfactional.com/.*  . now, why is that bad? because it breaks the admin app. why? obviously, my urls.py has the admin at 'admin/.*'. in the login form (for example, it happens in all kinds of other places as well) the form thinks its running at the root of the webserver, so it posts to '/admin/' , which actually means ofri.webfactional.com/admin – but i don't have an app configured here.

as always, someone already bumped into this problem. but once i saw the solution proposed here (hack the admin templates. brrrr….), i was so horrified that i had to find a better solution.

the trick was to use mod_rewrite to put the missing part back in the request. it took me some time to figure out, because as you probably know mod_rewrite is voodoo. so i'm posting it here, hoping it would help someone in the future.

i had to add a virtual host clause to the httpd.conf file, and put the existing <location> inside it (marked red here):

<VirtualHost *:XXXX>
    ServerName ofri.webfactional.com
    DocumentRoot /home/ofri/webapps/django/myproject
    <Location "/">
        PythonHandler django.core.handlers.modpython
        PythonPath "['/home/ofri/webapps/django','/home/ofri/webapps/django/myproject','/home/ofri/webapps/django/lib/python2.5'] + sys.path"
        SetEnv DJANGO_SETTINGS_MODULE django.settings
        SetHandler python-program
        RewriteEngine On
        RewriteCond %{REQUEST_URI}  !^/django/.*
        RewriteRule   ^home/ofri/webapps/django/myproject(.*)$  /django$1 [L]
    </Location>
</VirtualHost>

where XXXX is the port number you can find in the Listen XXXX line you should have in the same file, a few lines above.

if this helps you, or if you have another solution (not including hacking the admin templates…) i'd love to know…

  1. i use webfaction, which is great. but i guess even great hosting can cause you trouble sometimes []
FacebookEmailDeliciousShare


תגובה אחת

1 Greg‏11.18.09 ‏בשעה 11:01 pm‏

Thanks for this! I got my site working because of your help!

השארת תגובה

ניתן להשתמש בתגיות הבאות: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>