modify flask url before routing modify flask url before routing flask flask

modify flask url before routing


I'd normalise the path before it gets to Flask, either by having the HTTP server that hosts the WSGI container or a proxy server that sits before your stack do this, or by using WSGI middleware.

The latter is easily written:

import refrom functools import partialclass PathNormaliser(object):    _collapse_slashes = partial(re.compile(r'/+').sub, r'/')    def __init__(self, application):        self.application = application    def __call__(self, env, start_response):            env['PATH_INFO'] = self._collapse_slashes(env['PATH_INFO'])                                                                                                          return self.application(env, start_response)

You may want to log that you are applying this transformation, together with diagnostic information like the REMOTE_HOST and HTTP_USER_AGENT entries. Personally, I'd force that specific application to generate non-broken URLs as soon as possible.

Look at your WSGI server documentation to see how to add in extra WSGI middleware components.