Replace your web2py/routes.py with this:

------------- begin routes.py-----------
try: config=open('routes.conf','r').read()
except: config=''
def auto_in(apps):
    routes=[
        ('/robots.txt','/welcome/static/robots.txt'),
        ('/favicon.ico','/welcome/static/favicon.ico'),
        ('/admin$a','/admin$a'),
        ]
    for a,b in [x.strip().split() for x in apps.split('\n') \
        if x.strip() and not x.strip().startswith('#')]:
        if not b.startswith('/'): b='/'+b
        if b.endswith('/'): b=b[:-1]
        app = b.split('/')[1]
        routes+=[
            ('.*:https?://(.*\.)?%s:$method /' % a,'%s' % b),
            ('.*:https?://(.*\.)?%s:$method /static/$a' % a,'%s/static/
$a' % app),
            ('.*:https?://(.*\.)?%s:$method /appadmin/$a' % a,'%s/
appadmin/$a' % app),
            ('.*:https?://(.*\.)?%s:$method /$a' % a,'%s/$a' % b),
            ]
    return routes

def auto_out(apps):
    routes=[]
    for a,b in [x.strip().split() for x in apps.split('\n') \
        if x.strip() and not x.strip().startswith('#')]:
        if not b.startswith('/'): b='/'+b
        if b.endswith('/'): b=b[:-1]
        app = b.split('/')[1]
        routes+=[
            ('%s/static/$a' % app,'static/$a'),
            ('%s/appadmin/$a' % app, '/appadmin/$a'),
            ('%s/$a' % b, '/$a'),
            ]
    return routes

routes_in=auto_in(config)
routes_out=auto_out(config)
------------------- END ---------------

what does it do? It writes routes for you based on a simpler routing
configuration file called routes.conf. here is an example:

----- BEGIN routes.conf-------
127.0.0.1       /examples/default
domain1.com /app1/default
domain2.com /app2/default
domain3.com /app3/default
----- END ----------

It maps a domain (the left had side) into an app and it shortens the
URLs for the app, by removing the listed path prefix. That means

http://domain1.com/index will be mapped into  /app1/default/index
http://domain2.com/index will be mapped into  /app2/default/index

It is safe in that it preserves admin, appadmin, static files,
favicon.ico and robots.txt.

http://domain1.com/favicon.ico
http://domain1.com/robots.txt
http://domain1.com/admin/...   /admin/...
http://domain1.com/appadmin/...  /app1/appadmin/...
http://domain1.com/static/...  /app1/static/...

and vice-versa.

It does assume one app per domain.

I think something like this should be default since lots of people
find routes.py hard to work with.
Comments? Suggestions?

Massimo

Reply via email to