Braek
Braek

Reputation: 601

Apache: redirect to WSGI script is wrong?

I deployed a Flask application to Apache. This works, but I have added something to redirect non-www traffic to the www domain.

<VirtualHost *:80>
    ServerName myapp.com
    WSGIDaemonProcess myapp python-home=/home/ubuntu/envs/myapp user=ubuntu group=ubuntu threads=5
    WSGIScriptAlias / /home/ubuntu/apps/myapp/myapp.wsgi
    Alias /static /home/ubuntu/apps/myapp/myapp/static
    <Directory /home/ubuntu/apps/myapp>
        WSGIProcessGroup myapp
        WSGIApplicationGroup %{GLOBAL}
        Require all granted
        AllowOverride All
        RewriteEngine On
        RewriteBase /
        RewriteCond %{HTTP_HOST} !^www\. [NC]
        RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    </Directory>
</VirtualHost>

This works! But, when I surf to myapp.com/test.html I am redirected to www.myapp.com/myapp.wsgi/test.html - this is not correct! I tried modifying some parameters in the file above, but currently without the desired result.

Any tips on how to solve this?

Kind regards, B.

Upvotes: 1

Views: 961

Answers (1)

Braek
Braek

Reputation: 601

I managed to get things working with the following configuration:

<VirtualHost *:80>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    ServerName myapp.com
    WSGIDaemonProcess myapp python-home=/home/ubuntu/envs/myapp user=ubuntu group=ubuntu threads=5
    WSGIScriptAlias / /home/ubuntu/apps/myapp/myapp.wsgi
    Alias /static /home/ubuntu/apps/myapp/myapp/static
    <Directory /home/ubuntu/apps/myapp>
        WSGIProcessGroup myapp
        WSGIApplicationGroup %{GLOBAL}
        Require all granted
    </Directory>
</VirtualHost>

Upvotes: 1

Related Questions