WebFaction
Community site: login 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:

FORCE_SCRIPT_NAME = '/hour'

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

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 12 Feb '11, 13:25

amacinho
1057
accept rate: 0%


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

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.

permanent link

answered 12 Feb '11, 13:48

johns
5.4k312
accept rate: 23%

edited 12 Feb '11, 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?

(12 Feb '11, 14:00) amacinho

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!

(12 Feb '11, 14:18) amacinho

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,

http://morethanseven.net/2009/02/10/append-slashes-urls-django.html
(12 Feb '11, 14:29) johns
Your answer
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

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×909
×225
×76
×62

question asked: 12 Feb '11, 13:25

question was seen: 6,045 times

last updated: 12 Feb '11, 14:29

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM