Search code examples
pythonurl-rewritingwsgipep

Does PEP 3333 specification preclude Python WSGI Web Servers from having Rewrite Rules?


For the server part of Python's WSGI specification, not the application part, is the server not allowed to rewrite requests? I ask because I can't find any that do. As I understand it Nginx and Apache are generic so they will have a rewrite rule engine (and IIS). Why wouldn't a WSGI capable server have the same so that I do not have to use regex? Does PEP-333 not allow rewrites? Is rewriting not a Python way to do things?


Solution

  • How paths are handled is not part of the WSGI server responsibility. It passes on the request to the apps, transparently, and returns the response produced, equally transparently. That is what the standard describes.

    URL rewriting is an extra service that a server could implement, but that has nothing to do with the WSGI standard. Apache does this with an optional module (mod_rewrite). It is a popular feature so other servers have offered it too. But that doesn't mean that the WSGI standard needs to say anything about this, just as the HTTP standard doesn't describe URL rewriting.

    If you need paths rewritten, you can easily do so in your WSGI app, or write WSGI middleware to do so. WSGI middleware looks just like a WSGI app to the server, and like a server to the next app.

    Someone already wrote such middleware for you, see the WSGIRewrite project.