login community faq

Hi,

I have a Django (WSGI) application running under a subdirectory. The FORCE_SCRIPT_NAME is set to the directory name in settings.py:

1
FORCE_SCRIPT_NAME = '/hour'

The location hour is aliased to my Django application in httpd.conf:

1
WSGIScriptAlias /hour /home/buradayiz/webapps/twitter_dev/hour.wsgi

The URL http://mydomain.com/hour/ works correctly. But the URL without the trailing slash (http://mydomain.com/hour) is problematic: It looks like the working directory of my scripts becomes one directory higher, and the relative paths in my template files do not work anymore.

I believe the solution should be in the Apache Rewrite rules because I cannot distinguish between the two URLs (with or without trailing slash) in Django in urls.py.

Can anyone help?

Thanks.

asked Feb 12 '11 at 13:25

amacinho's gravatar image

amacinho
85


This series of rules will automatically redirect domains without a tailing slash to domains with a tailing slash,

1
2
3
4
5
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]

You may have to adjust them slightly to work for your django app. They should be placed within the httpd.conf file.

answered Feb 12 '11 at 13:48

johns's gravatar image

johns ♦♦
340427

edited Feb 12 '11 at 13:53

Thanks, but it doesn't work. Looks like httpd.conf is not the correct place for these rules. I get the following error

RewriteBase: only valid in per-directory config files

Any ideas?

(Feb 12 '11 at 14:00) amacinho amacinho's gravatar image

Silly me. I removed the RewriteBase line and now it works. In order to protect URIs with query parameters from being appended, I also added the following condition: RewriteCond %{QUERY_STRING} !.+$ Thanks!

(Feb 12 '11 at 14:18) amacinho amacinho's gravatar image

Your welcome. Glad to hear it is working out. Django can be picky about URL redirecting and setting it in urls.py may be a better solution, but I have not tested it myself. A quick Google search for "django url redirect tailing slash" lead me to this great blog entry,

1
http://morethanseven.net/2009/02/10/append-slashes-urls-django.html
(Feb 12 '11 at 14:29) johns ♦♦ johns's gravatar image
Your answer
If you have an answer to the above question, then use the form below. Otherwise, use the appropriate 'add new comment' button above to post your feedback.
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×643
×149
×45
×44

Asked: Feb 12 '11 at 13:25

Seen: 1,647 times

Last updated: Feb 12 '11 at 14:29

Plans & prices    Sign up    Why WebFaction?    Contact us    Affiliate program    Support    Legal    Jobs    Blog    Control panel login
Powered by OSQA
© Copyright 2003-2012 Swarma Limited - WebFaction is a service of Swarma Limited