Sudhir Jonathan Sudhir Jonathan - 3 months ago 16
Python Question

How do I make a trailing slash optional with webapp2?

I'm using the new webapp2 (now the default webapp in 1.6), and I haven't been able to figure out how to make the trailing slash optional in code like this:

webapp.Route('/feed', handler = feed)


I've tried
/feed/?
,
/feed/*
,
/feed\/*
and
/feed\/?
, all to no avail.

Answer

To avoid creating duplicate URL:s to the same page, you should use a RedirectRoute with strict_slash set to True to automatically redirect /feed/ to /feed, like this:

from webapp2_extras.routes import RedirectRoute

route = RedirectRoute('/feed', handler=feed, strict_slash=True)

Read more at http://webapp-improved.appspot.com/api/webapp2_extras/routes.html

Comments