[Web-SIG] handling URLs with ending slash

Manlio Perillo manlio_perillo at libero.it
Sun Dec 14 20:28:45 CET 2008

Thomas Broyer ha scritto:
> 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)

I not sure I like this.

> or eventually add a WSGI middleware to each mapped application

The URL dispatcher is a WSGI middleware, so it is ok for me to do this 
in the url dispatcher.
I would like to keep the numbers of middleware to a minimun (function 
calls in Python are not cheap).

> (...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))

Yes, that's an idea.
Note that this is a special case of an "URL normalizer" middleware.
A middleware can be used as a function decorator.
This is a point in favour to use a dedicated middleware for this.

Thanks   Manlio Perillo

More information about the Web-SIG mailing list