I want to implement structure in Flask, which can handle multiple domains.
So when I type in browser “http://domain1.com/show/1“, it actually executes function with routing like
@app.route('<string:domain>/show/<int:id>')
def show(domain = '', id = ''):
return 'Domain is ' + domain + ', ID is ' + str(id)
And it is very important, the URL in client’s browser should be still “http://domain1.com/show/1“. And as I know, when using redirect in Flask, it changes url. How should I organise such structure? Thanks!
The
requestobject already has aurl_rootparameter. Or you can use theHostheader:If you need to redirect within the application,
url_rootis the attribute to look at, as it’ll include the full path for the WSGI application, even when rooted at a deeper path (e.g. starting athttp://domain1.com/path/to/flaskapp).It probably is better still to use
request.url_for()to have Flask generate a URL for you; it’ll takeurl_rootinto account. See the URL Building documentation.