I want to have my top-level domain as a portal for various subdomains that correspond to different sections of my site. example.com
should route to a welcome.html
template. eggs.example.com
should route to an "eggs" subsection or application of the site. How would I achieve this in Flask?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
@app.route()
takes asubdomain
argument to specify what subdomain the route is matched on.Blueprint
also takes asubdomain
argument to set subdomain matching for all routes in a blueprint.You must set
app.config['SERVER_NAME']
to the base domain so Flask knows what to match against.As of Flask 1.0 you must also set
subdomain_matching=True
when creating the app object.When running locally, you'll need to edit your computer's hosts file (
/etc/hosts
on Unix) so that it will know how to route the subdomains, since the domains don't actually exist locally.Remember to still specify the port in the browser,
http://example.com:5000
,http://eggs.example.com:5000
, etc.Similarly, when deploying to production, you'll need to configure DNS so that the subdomains route to the same host as the base name, and configure the web server to route all those names to the app.
Remember, all Flask routes are really instances of
werkzeug.routing.Rule
. Consulting Werkzeug's documentation forRule
will show you quite a few things that routes can do that Flask's documentation glosses over (since it is already well documented by Werkzeug).