Is there a way to remove application name from URL()?

648 views
Skip to first unread message

Bob

unread,
Jan 17, 2011, 6:15:24 AM1/17/11
to web2py-users
I'm using a routing file which makes my app load by default on
localhost. It works well, but everywhere I have used the URL()
function it prepends the application name to the URL. This of course
no longer works, because my real URLs no longer need the name.

So is there any configuration that will tell URL() not to add app name
automatically?

In case of need, here's my routes.py:

---
default_application = "myapp"

routes_in = (
('/static/$anything', '/myapp/static/$anything'),
('/appadmin/$anything', '/myapp/appadmin/$anything'),
('/favicon.ico', '/myapp/static/favicon.ico'),
('/robots.txt', '/myapp/static/robots.txt'),
('/(?P<any>.*)', '/myapp/\g<any>'),
)
routes_out = [(x, y) for (y, x) in routes_in[:-2]]
----

Bob

unread,
Jan 17, 2011, 4:06:46 PM1/17/11
to web2py-users
In attempt to get some response I'll try to clarify/simplify my
question.

After rewriting with routes.py I have my app accessible as:

http://127.0.0.1/

But all the internal links, created with the URL() function come as:

http://127.0.0.1/myapp/

And obviously they don't work.

Is there no way to tell the URL() function not to prepend app name to
the URLs?

howesc

unread,
Jan 17, 2011, 4:11:00 PM1/17/11
to web...@googlegroups.com
i'm pretty sure there is a section in the book that covers this topic exactly.  there is also new versions of routing since i last wrote this.  anyhow, my routes info:

routes_in = (('/admin(?P<f>.*)', '/admin$f' ),  #make the admin app work
             ('/','/ec'), #/ goes to the ec app
             ('.*:/favicon.ico','/ec/static/images/favicon.ico'), #fav icon for all things on this server
             ('/$c/(?P<f>.*)', '/ec/$c/$f'), # remove the leading /ec from our URLs
             ('/$c', '/ec/$c')) #remove leading /ec, don't require a function name (default to index)

routes_out = (('/admin/(?P<f>.*)', '/admin/$f' ), # don't rewrite admin links
              ('/$a/static/(?P<f>.*)', '/$a/static/$f'), #don't rewrite static links
              ('/$a/$c/index.html', '/$c/'), # removing leading /ec, and the index function if there are no params
              ('/$a/$c/index', '/$c/'), # removing leading /ec, and the index function if there are no params
              ('/$a/$c/(?P<f>.*)', '/$c/$f')) # remove leading /ec

note that your routes_out must remove the application from the URL generated by URL() for it to work.  "ec" is my application name in this example.  you may also wish to look at the more recent updates to the routing mechanism, they might be easier to use - i don't know myself yet.

cfh

David J.

unread,
Jan 17, 2011, 4:14:11 PM1/17/11
to web...@googlegroups.com
The easy way is to use "init" as your application name;

It uses that by default.

HTH.

Jonathan Lundell

unread,
Jan 17, 2011, 5:47:44 PM1/17/11
to web...@googlegroups.com

As David mentioned, you can rename your app 'init' and get some of this to happen. It won't delete all mention of the app name, though--only when it can also delete the controller and function name.

If you don't want to change your app name, you can create (in web2py's base directory) a file named routes.py with nothing in it but this line:

default_application = 'myapp'

But to get the app name deleted on a more systematic basis, you'll need to write a more elaborate routes.py. There's a new version coming along, in beta in the trunk right now, that should be available for more general use pretty soon, so if you're not in a hurry, just go with one of the above solutions for now, and when the new rewrite logic is ready for prime time you can add a couple more lines in routes.py and be done with it.

If you're in more of a hurry, use the existing rewrite logic; it's not going away, but it's a little trickier to set up.

Bob

unread,
Jan 18, 2011, 3:04:49 AM1/18/11
to web2py-users
Thanks mates, I'm going to just use init for now, will wait for the
new routing for the future

Albert Abril

unread,
Jan 18, 2011, 6:25:21 AM1/18/11
to web...@googlegroups.com
Maybe request.application would help. Is a reference, as the name says, to the actual application.

Wikus van de Merwe

unread,
Feb 1, 2011, 11:11:12 AM2/1/11
to web...@googlegroups.com
Your routes do not work for internal links because the last rewriting rule from routes_in have to be also applied to routes_out.


routes_in = (
  ('/static/$anything', '/myapp/static/$anything'),
  ('/appadmin/$anything', '/myapp/appadmin/$anything'),
  ('/favicon.ico', '/myapp/static/favicon.ico'),
  ('/robots.txt', '/myapp/static/robots.txt'),
  ('/(.*))', r'/myapp/\1'),
)
routes_out = [(x, y) for (y, x) in routes_in[:-2]]+[('/myapp/(.*))', r'/\1')]

Reply all
Reply to author
Forward
0 new messages