Search code examples
pythongoogle-app-enginewebapp2

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.


Solution

  • 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://webapp2.readthedocs.io/en/latest/api/webapp2_extras/routes.html