Subdomain in Flask | Python (original) (raw)

Last Updated : 18 Mar, 2026

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:

**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.

Python `

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:

Run the app and notice the link on which the app is running.

subdomain-1

Snapshot of the Terminal

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-

  1. **basic: An endpoint with extension to the path on the main domain.
  2. **practice: An endpoint serving on the practice subdomain.
  3. **courses: An endpoint with extension on to the path on the practice subdomain. Python `

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:

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.