Prerequisite: Introduction to Flask
In this article, we will learn how to setup subdomains in Flask. But first, let’s go through the basic like what is DNS and subdomains.
Domain Name System (DNS):
The Domain Name System (DNS) is a hierarchical and decentralized naming system for computers, services, or other resources connected to the Internet or a private network. Most prominently, it translates more readily memorized domain names to the numerical IP addresses needed for locating and identifying computer services and devices with the underlying network protocols.
DNS is basically using words (Domain Names) in place of numbers (IP addresses) to locate something. For example,
127.0.0.1 is used to point the local computer address, localhost.
A subdomain is a domain that is part of a larger domain. Basically, it’s a sort of child domain which means it is a part of some parent domain. For example,
contribute.geeksforgeeks.org are subdomains of the
geeksforgeeks.org domain, which in turn is a subdomain of the
org top-level domain (TLD).
These are different from the path defined after TLD as in
Further, we will discuss how to set endpoints in your web application using Python’s micro-framework, Flask.
Adding alternate domain name for local IP –
Prior to the coding part, we got to setup hosts file in order to provide alternate names to local IP so that we are able to test our app locally. Edit this file with root privileges.
Linux: /etc/hosts Windows: C:\Windows\System32\Drivers\etc\hosts
Add these lines to set up alternate domain names.
127.0.0.1 vibhu.gfg 127.0.0.1 practice.vibhu.gfg
In this example, we’re considering
vibhu.gfg as our domain name, with
gfg being the TLD.
practice would be a subdomain we’re targeting to set in our web app.
Setting up the Server –
In the app’s configuration
SERVER_NAME is set to the domain name, along with the port number we intend to run our app on. The default port, flask uses is
5000, so we take it as it is.
Run the app and notice the link on which the app is running.
Test the link on your browser.
Adding Several Endpoints –
- basic: An endpoint with extension to the path on the main domain.
- practice: An endpoint serving on the
- courses: An endpoint with extension on to the path on the practice subdomain.
Subdomains in Flask are set using the
subdomain parameter in the
- Python | Using for loop in Flask
- Python | 404 Error handling in Flask
- Todo list app using Flask | Python
- Python | Build a REST API using Flask
- Create a Weather app using Flask | Python
- Python | Introduction to Web development using Flask
- Program to get the subdomain of a URL using PHP
- Flask - (Creating first simple application)
- Setup API for GeeksforGeeks user data using WebScraping and Flask
- Important differences between Python 2.x and Python 3.x with examples
- Python | Merge Python key values to list
- Python | Index of Non-Zero elements in Python list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to Python Libraries
- Python | Sort Python Dictionaries by Key or Value
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.