Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to setup my Django app to run under Apache with modwsgi. I have defined the following virtualhost:

<VirtualHost *>

    WSGIScriptAlias / /home/domain/apache/django.wsgi

    <Directory /home/domain/apache>
    Order deny,allow
    Allow from all

    Options -Indexes +FollowSymlinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^$ [NC]
    RewriteRule ^(.*)$$1 [QSA,L,R=301]

The rewrite rules at the end are there to make the canonical site name. There is something wrong though. When I browse to >, I get redirected to > Browsing to > works just fine.

Am I missing something in my configuration? Thanks.

Update: I changed the RewriteRule to

RewriteRule ^django.wsgi/(.*)$$1 [L,R=301]. 

This change fixes the issue. I will appreciate it if someone can elaborate on why the /django.wsgi/ part ends up in the URL. Thanks!

share|improve this question
up vote 1 down vote accepted

Your original rewrite rule likely confused things because the $1 pattern that matched already had a '/' in it, thus you created a double slash. What happens if you use:

RewriteRule ^(.*)$$1 [QSA,L,R=301]

and remove your extra rule?


You have misinterpreted what I meant. I wasn't talking about your workaround, but the original rule.

To make it more obvious what you are doing wrong and which is potentially causing issues, use:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^/(.*)$$1 [L,R=301]

Note how I have explicitly listed the '/' outside of the sub pattern. Only by doing that would it be okay to use a '/' before '$1'. If you don't you will get repeating slashes.

I'd also suggest dropping QSA. Examples I see elsewhere on the Internet don't use that either.

Finally, you should escape the '.' in the host name being matched else could technically match stuff other than a literal '.' as well.

So, try that. If still issues, then you should enable rewrite module logging and see what is going on. You possibly have some other rewrite rules in your configuration which are causing issues.


Hmmm, finally realised your actual problem is that you have the rewrite rule inside of the Directory directive container instead of outside.

So, move the rewrite rules to immediately inside of the VirtualHost and not the Directory directive.

share|improve this answer
I missed the '/' in my update. I just edited my question to fix the error. – user31196 Jan 10 '10 at 23:13
Thank you for your help. That was indeed the problem. – user31196 Jan 11 '10 at 22:48

It's happening because your web server is setting SCRIPT_NAME to "django.wsgi". If add FORCE_SCRIPT_NAME="" to your, you should be able to do without the RewriteRule.

share|improve this answer
There should never be a need to use FORCE_SCRIPT_NAME with mod_wsgi. – Graham Dumpleton Jan 9 '10 at 20:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.