On Dec 9, 2010, at 5:59 PM, pbreit wrote:
> What's the preferred way to clean up the URLs? For example, I never want
> "/myapp/default/index" to display.
>
> /myapp is OK during development and/or when I'm on a server with multiple
> apps. But when I'm on a domain/server with one app, I want
> "http://myserver.com/" to go to my home page without showing the path.
>
> Then, I think what I want is: if there are 0 segments, it routes to
> /myapp/default/index. If one segment, it routes to /myapp/default/segment1.
> If 2 segments, /myapp/segment1/segment2. And also handle where the last
> segment is an ID.
>
> /index should never display and simply be implied by "/".
>
> Would I need routes for all of this? I know I can set myapp to "init" or
> "welcome" (or change the default app name) but that leads to the whole path
> showing.
That won't show the whole path to the extent that the path is the default one,
but in order to handle segments as you describe, you need to use routes.
Here's routes.py from one of my apps:
routes_in = (
('/?', '/vpepm/'),
('/(?P<app>(admin|examples|welcome|vpepm))','/\g<app>/'),
('/(?P<app>(admin|examples|welcome|vpepm))/(?P<any>.*)','/\g<app>/\g<any>'),
('/favicon.ico', '/vpepm/static/img/favicon.ico'),
('/robots.txt', '/vpepm/static/robots.txt'),
('/(?P<ctlr>(sysadmin|appadmin|css|static|init\\b))(?P<any>.*)',
'/vpepm/\g<ctlr>\g<any>'),
('/(?P<any>.*)','/vpepm/default/\g<any>'),
)
routes_out = (
('/(?P<app>(admin|examples|welcome))/(?P<any>.*)','/\g<app>/\g<any>'),
('/vpepm(/default)?/?', '/'),
('/vpepm(/default(/index)?)?/?', '/'),
('/vpepm/default/(?P<any>.*)','/\g<any>'),
('/vpepm/(?P<any>.*)','/\g<any>'),
)
The name of the app is (obviously) "vpepm". I want to be able to access the
standard web2py-supplied apps as well, so you see their names. In order to
distinguish controller-segments from function-segments, routes needs to know
what vpepm's controllers are, which you see in the penultimate pattern of
routes_in. So if I specify a just a function (which can't collide with my
controller names), it's treated as segment2 in my default controller.