![]() |
VOOZH | about |
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).
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.
Flask requires a SERVER_NAME setting for subdomains to work. This includes both the domain name and the port number.
Explanation:
Run the app and notice the link on which the app is running.
Test the link on your browser.
👁 ImageWe’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-
practice subdomain.Explanation:
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.
👁 Image