A subdomain is a child domain that’s part of a larger domain. For example, practice.geeksforgeeks.org and write.geeksforgeeks.org are subdomains of the geeksforgeeks.org domain, which in turn is a subdomain of the org top-level domain (TLD).
Setting Up Host Entries
Before writing any Flask code, you need to give your local IP (127.0.0.1) alternate domain names for testing subdomains on your machine. Steps to do it:
Step 1: Open Notepad with administrator access and open the host file of your system in it make edits, path to the host file for:
- Linux: /etc/hosts
- Windows: C:\\Windows\\System32\\Drivers\\etc\\hosts
Step 2: Add entries for your custom domain and subdomain in the end of the host file and save changes:
127.0.0.1 vibhu.gfg
127.0.0.1 practice.vibhu.gfg
Here, vibhu.gfg is our main domain, and practice.vibhu.gfg is the subdomain we’ll use in the Flask app.
Configuring the Server
Flask requires a SERVER_NAME setting for subdomains to work. This includes both the domain name and the port number.
from flask import Flask
app = Flask(__name__)
app.config['SERVER_NAME'] = 'vibhu.gfg:5000'
@app.route('/')
def home():
return "Welcome to GeeksForGeeks !"
if __name__ == "__main__":
app.run()
Explanation:
- website_url is set to prajjwal.gfg:5000, matching the domain name and the Flask default port.
- app.config['SERVER_NAME'] tells Flask to use prajjwal.gfg as the main domain.
Run the app and notice the link on which the app is running.

Test the link on your browser.

Creating Multiple Endpoints with Subdomains
We’ll add three routes on the main domain (vibhu.gfg) and subdomain (practice.vibhu.gfg). Subdomains in Flask are defined using the subdomain parameter in the @app.route decorator. The routes are-
- basic: An endpoint with extension to the path on the main domain.
- practice: An endpoint serving on the
practicesubdomain. - courses: An endpoint with extension on to the path on the practice subdomain.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Welcome to GeeksForGeeks !"
@app.route('/basic/')
def basic():
return "Basic Category Articles listed on this page."
@app.route('/', subdomain='practice')
def practice():
return "Coding Practice Page"
@app.route('/courses/', subdomain='practice')
def courses():
return "Courses listed under practice subdomain."
if __name__ == "__main__":
website_url = 'vibhu.gfg:5000'
app.config['SERVER_NAME'] = website_url
app.run()
Explanation:
- @app.route('/basic/'): Creates a path-based endpoint on the main domain (e.g., vibhu.gfg:5000/basic/).
- @app.route('/', subdomain='practice'): Points the root path (/) to the subdomain practice.vibhu.gfg, displaying “Coding Practice Page.”
- @app.route('/courses/', subdomain='practice'): Uses the same subdomain (practice.vibhu.gfg) but with a path /courses/.
Running the Application
Run the flask app using command- "python app.py" in terminal and visit the development url - "http://vibhu.gfg:5000/". To navigate to subdomains of our app, visit url- "http://practice.vibhu.gfg:5000/". Below is the snapshot of the subdomain page.
