Apr 17
web2py behind apache with mod_wsgi part II: mod_rewrite rules
Ok, in the previous post I’ve shown a way to run web2py behind apache using mod_wsgi.
The main “problem” still to be addressed is: URIs. Assume you have 3 website which implies (in the common case) 3 web2py application,
- www.primo.it with the we2bpy app called primo
- www.secondo.it , appname is secondo
- www.terzo.it, appname is terzo.
(ok, maybe I lack of fantasy … )
Now we ended up having a vhost with
ServerName web2py.localhost
so all of our 3 applications are at
web2py.localhost/primo/default/index, web2py.localhost/secondo/default/index, web2py.localhost/terzo/default/index. It should work, but it’s not what we want.
The simplest thing here is to add
ServerAlias www.primo.it www.secondo.it www.terzo.it
to the vhost conf file. This works, but has a couple of disadvantages:
- application name is still in the url (i.e. www.primo.it/primo/default/index)
- secondo and terzo are available from primo’s url and vice-versa (www.primo.it/secondo/default/index shows secondo application)
Here enters mod_rewrite. Leave the ServerAlias in place, as they are needed.
Disclaimer I’m **NOT** a mod_rewrite guru -as you’ll see-, and I really think that there are better ways to accomplish this task: suggestions are welcome ^^
Starting with www.primo.it, add this at the beginning of the vhost file, just after the DocumentRoot directive:
RewriteEngine On
RewriteCond %{HTTP_HOST} =www.primo.it [NC,OR]
RewriteCond %{HTTP_HOST} =primo.it [NC]
RewriteRule /primo/(.*) /$1 [N]
RewriteCond %{HTTP_HOST} =www.primo.it [NC,OR]
RewriteCond %{HTTP_HOST} =primo.it [NC]
RewriteRule ^/(.*) /primo/$1 [PT,L]
The first “block” is needed to fix “links” in web2py applications, so that if you are using URL() function it continues to work. The problem it addresses is that we don’t want application name in URLs buth URL() keeps adding it. So we remove it; the [N] flag after the first RewriteRule means “restart from the beginning after applying” (the downside is that this is an external redirect
)
The second “block” is needed to do the actual work: it transparently add the app name to the request, but the url in user browser remains without. So one user accessing www.primo.it/default/index arrives to wsgi as if he/she was calling www.primo.it/primo/default/index (and the user still have www.primo.it/default/index) in his/her location bar. The [L] flags means “last” (i.e. stop processing rewrites) and [PT] means “jump directly to the next alias handler”, which is the WSGIScriptAlias defined in previous posts.
Repeat this two blocks (RewriteEngine On is needed only the first time) for every application you have (you can do includes in separate files if you wish) and you end up having
- www.primo.it that redirect to www.primo.it/default/index.
- www.primo.it/secondo/default/index still working
- web2py.localhost/primo/default/index still working
- https (i.e. management) only on web2py.localhost. If you want https support for primo.it you should add the rewrite rules event to the https section, (or -better- include two times the same file)
Hope this helps… and if you have better options or ideas write a comment
Last thing about the RewriteCond: yes, I already know that it can be done with a regex instead of using the =www.primo.it AND rewriting the condition without www. but I like this way far more as it’s simpler and much more understandable.
