Flask app that routes based on subdomain

sw00 picture sw00 · Sep 22, 2011 · Viewed 7k times · Source

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?

Answer

Sean Vieira picture Sean Vieira · Sep 24, 2011

@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).