Having recently completed my own personal URL shortener app on web2py, the
trick was using pattern based routes at the root level. Actually, even for
a fairly complex work-related app we had to use the root-level routes.py as
well. Specially with an URL shortener, you want to have the URL as small as
possible (else it sorta defeats the purpose), so you are going to need
something along the lines of:
routes_in = (
(r'^/?$', r'/app_name/default/index'),
(r'^/(?P<url>[^/]*)/?$', r'/app_name/default/index/\g<url>'),
)
in your root-level routes.py The drawback is that you will lose access to
all other apps (including admin) but that can be a good thing for public
deployments.
On Thursday, March 29, 2012 12:40:16 PM UTC-4, Simon Bushell wrote:
>
> hi all,
>
> So I am coding a little app that acts as a sort of url shortener (like
> bit.ly).
>
> Visiting *myapp.com/tcgata* will search my database for an URL linked to '
> tcgata'
>
> My reading of the documentation suggests that this can be achieved using
> pattern-based URL rewriting in routes.py. As such I made the following file:
>
> routes_in = (
> ('.*[a-zA-Z0-9]{6}', '/catchAll'),
> )
>
> #in default.py
> def catchAll():pass
>
>
> where catchAll is a controller function that will glean the search string
> ('tcgata') from the request object and redirect to the relevant URL (or 404
> if not found). Currently it is a passed function.
>
> However, the redirection doesn't seem to be working for me. When I visit
> myapp.com/tcgata i get an error page saying *'invalid function
> (default/tcgata)'*.
>
> Where am I going wrong? Is it a routing issue? a regex issue? I am sure it
> is something simple I am missing, but I think I need some extra eyes to
> find it.
>
> Furthermore, if there is an easier way to do what I am seeing to achieve,
> I am all ears!
>
> thanks in advance
>
> Simon
>
>
>
>