pythonhttp-redirectflask

Flask redirect with same url and different host?


I'm using flask as web server.

Currently, I have a request: http://host-a.com/test1/?a=1&b=2 and I want to redirect to http://host-b.com/test1/?a=1&b=2 and get the response.

Now, I have a lot of such url as test1, test2 and so on. I just want to redirect the host. How to deal with it?

edited: There are many urls need to be redirected, while there are also many urls can't be redirected.

For example: http://host-a.com/test1/?a=1&b=2 need, but http://host-a.com/real1/?a=1&b=2 can't


Solution

  • So perhaps this will help you:

    from flask import Flask, redirect
    app = Flask(__name__)
    
    @app.route('/', defaults={'path': ''})
    @app.route('/<path:path>')
    def catch_all(path):
        new_path = 'http://host-b.com/' + path
        return redirect(new_path, code=302)
    
    if __name__ == '__main__':
        app.run()