Reputation: 606
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!
Upvotes: 21
Views: 19583
Reputation: 2115
Here's what the code looks like with the import:
import flask
print flask.request.url_root # prints "http://domain1.com/"
print flask.request.headers['Host'] # prints "domain1.com"
Upvotes: 2
Reputation: 1122202
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.
Upvotes: 35