Flask app that routes based on subdomain


Question

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?

1
12
8/16/2018 9:19:28 PM

Accepted Answer

@app.route() takes a subdomain argument to specify what subdomain the route is matched on. Blueprint also takes a subdomain 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. You will also need to specify the port, unless your app is running on port 80 or 443 (i.e in production).

As of Flask 1.0 you must also set subdomain_matching=True when creating the app object.

from flask import Flask

app = Flask(__name__, subdomain_matching=True)
app.config['SERVER_NAME'] = "example.com:5000"

@app.route("/")
def index():
    return "example.com"

@app.route("/", subdomain="eggs")
def egg_index():
    return "eggs.example.com"
ham = Blueprint("ham", __name__, subdomain="ham")

@ham.route("/")
def index():
    return "ham.example.com"

app.register_blueprint(ham)

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.

127.0.0.1 localhost example.com eggs.example.com ham.example.com

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 for Rule 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).

29
4/29/2019 9:49:10 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon