[Web-SIG] handling URLs with ending slash

Thomas Broyer t.broyer at gmail.com
Sun Dec 14 15:45:13 CET 2008

On Sun, Dec 14, 2008 at 11:23 AM, Manlio Perillo wrote:
> In my WSGI applications I always have an ending slash to the URLs.
> This means that an URL without the ending slash will cause the underlying
> resource to return 404 Not Found HTTP response.
> What is the best method to handle this, using a regex based URL dispatcher?

I would add some kind of "catch-all entry" to dispatch to a "trailing
slash redirector" WSGI app:

   routes.add("[^/]$", force_trailing_slash)

or eventually add a WSGI middleware to each mapped application
(...that need such a treatment, could be all of them) that would issue
a redirect to the "slash-appended" URL when needed, or just pass
through to the application otherwise:

   routes.add(<regexp>, force_trailing_slash(my_application))

> I'm planning to add an option to my URL dispatcher to force any URL to have
> an ending slash (as an example requesting an HTTP redirect - either 302 or
> 301, or by just internally modifying the URL), but I'm not sure this is the
> best solution.

A 301 is what webservers (such as Apache) generally do for resources
served from file systems (when asked for a directory/folder).
Internally modifying the URL could easily yield bad results, as it has
an impact on relative URLs resolution.

Anyway, I wouldn't build it inside the dispatcher (just a matter of
taste; it's useful, whichever dispatcher you're using)

Thomas Broyer

More information about the Web-SIG mailing list