The following Apache conf file serves a WSGI application via port 9002:

<VirtualHost *:9002>
    SSLEngine on
    SSLCertificateFile /etc/apache2/ssl/apache.crt
    SSLCertificateKeyFile /etc/apache2/ssl/apache.key

    WSGIDaemonProcess matching-server user=ubuntu group=ubuntu threads=1
    WSGIScriptAlias / /var/www/matching-server/server.wsgi

    <Directory /var/www/matching-server>
        WSGIProcessGroup matching-server
        WSGIApplicationGroup %{GLOBAL}
        AllowOverride AuthConfig
        Order deny,allow
        Allow from all

The WSGI server responds to /metadata call, and returns a JSON with uptime information. I would like to alias /api to /metadata, so that calls to /api will be re-written as calls to /metadata and sent to the internal WSGI server.

I have enabled mod_rewrite on the Ubuntu server:

sudo a2enmod rewrite

And added the following rewrite rule to the conf file:

RewriteEngine On
RewriteRule    ^api/?$    /metadata    [NC,L]

I tried adding the rewrite rule after the SSL section and inside the Directory section, but I am still getting a Not Found error when accessing /api:

Not Found

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

Needless to mention, /metadata works without problems with or without the redirect rule.

Any idea what's wrong with my rewrite rule?

  • your rewrite rule should not be in your conf file, it should be in your htaccess file. Also if you have changed your apache.conf file restart apache after you have removed the rewrite and saved your file – Liam Sorsby Feb 4 '14 at 16:23
  • Are you sure? I tried placing it in the .htaccess file and got an Internal Server Error response. The error log output is .htaccess: RewriteEngine not allowed here. – Adam Matan Feb 5 '14 at 8:58
  • look here: stackoverflow.com/questions/3789692/… are you using apache or nginx? – Liam Sorsby Feb 5 '14 at 9:18
  • Ah wait sorry try this docs.silvacms.org/2.3/wsgi.html sorry i didn't see that it was WSGI – Liam Sorsby Feb 5 '14 at 9:19
  • even in 2014 why would you use .htaccess files for rewrites to application servers URIs like wsgi where all of the paths are virtual and have no meaningful mapping to the local filesystem? – cowbert Dec 8 '17 at 20:54

I needed a PT flag instead of [NC, L] in order to pass the result URL to WSGIScriptAlias:

RewriteEngine On
RewriteRule    ^/api$   /metadata    [PT]

Quoting the manual:

The target (or substitution string) in a RewriteRule is assumed to be a file path, by default. The use of the [PT] flag causes it to be treated as a URI instead. That is to say, the use of the [PT] flag causes the result of the RewriteRule to be passed back through URL mapping, so that location-based mappings, such as Alias, Redirect, or ScriptAlias, for example, might have a chance to take effect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.