Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

<VirtualHost *>
    ServerName www.domain.com
    ServerAlias domain.com

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

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

    Options -Indexes +FollowSymlinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
    RewriteRule ^(.*)$ http://www.domain.com/$1 [QSA,L,R=301]
    </Directory>
</VirtualHost>

The rewrite rules at the end are there to make www.domain.com the canonical site name. There is something wrong though. When I browse to >http://domain.com, I get redirected to >http://www.domain.com/django.wsgi/. Browsing to >http://www.domain.com works just fine.

Am I missing something in my configuration? Thanks.

Update: I changed the RewriteRule to

RewriteRule ^django.wsgi/(.*)$ domain.com/$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

2 Answers 2

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 ^(.*)$ http://www.domain.com$1 [QSA,L,R=301]

and remove your extra rule?


UPDATE 1

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 ^/(.*)$ http://www.domain.com/$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.


UPDATE 2

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 settings.py, you should be able to do without the RewriteRule.

http://docs.djangoproject.com/en/dev/ref/settings/#force-script-name

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

 
discard

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