Handling with multiple domains in Flask
The request
object already has a url_root
parameter. Or you can use the Host
header:
print request.url_root # prints "http://domain1.com/"print request.headers['Host'] # prints "domain1.com"
If you need to redirect within the application, url_root
is 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 at http://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 take url_root
into account. See the URL Building documentation.