Learning objectives
After reading this article you will be able to:
- Understand what a subdomain is and how it appears in DNS
- Create a subdomain with a CNAME or A record
- Choose between a subdomain and a subfolder for your project
You register example.com once. After that, you can create www.example.com, api.example.com and dashboard.example.com without registering anything new. Each of those extra names is a subdomain.
Subdomains are the standard way to split a project into parts: the website, the API, the docs, the staging environment. Each part can run on a different server, with its own certificate and its own rules.
What is a subdomain?#
A subdomain is a name created to the left of an existing domain. In DNS, a name reads right to left, and each dot-separated piece is a label:
api . example . com
| | |
| | TLD (top level)
| registered domain
subdomainThe registered domain (example.com) is the part you pay to register. Everything to its left is yours to organize. The concepts come from RFCs 1034 and 1035, which describe DNS as a tree where each node delegates the levels below it.
How does DNS treat a subdomain?#
Each subdomain can have its own DNS records. api.example.com does not have to point where www.example.com points.
A simplified zone file:
example.com. 3600 IN A 203.0.113.10
www.example.com. 3600 IN CNAME example.com.
api.example.com. 3600 IN A 203.0.113.20
blog.example.com. 3600 IN CNAME my-account.example-blog.net.Here the site and the API sit on different IPs, and the blog is served by an external provider. You can check the result with dig:
dig +short api.example.com AThe size limits come from RFC 1035: each label is up to 63 characters and the full name up to 255 octets in wire format (253 characters as text). The recommended hostname syntax (section 2.3.1) uses letters, digits and hyphens.
How do you create a subdomain?#
In the DNS panel of the provider that answers for your domain:
- Pick the record type. If the target is an IP, use
A(IPv4) orAAAA(IPv6). If the target is another name, such as an external service, useCNAME. - In the name field, type only the prefix (
api). Most panels append the domain for you. - Fill in the value (the IP or target name) and save.
- Wait for the TTL to expire in caches. See how DNS propagation works.
One detail that trips people up: a CNAME cannot coexist with other records at the same name (RFC 1912, section 2.4). Because the root domain (example.com) always has NS and SOA records, it cannot hold a CNAME. Subdomains can. Some DNS providers offer a proprietary record (ALIAS or ANAME) that imitates a CNAME at the root, but it is not part of the standard.
What about a wildcard subdomain?#
A *.example.com record answers for any name under the domain that does not exist in the zone (RFC 4592). It suits systems where each customer gets an address, like customer1.example.com. The trade-off is that mistyped names resolve too, and the TLS certificate must cover the names in use. See how SSL/TLS works.
Subdomain or subfolder?#
Subdomain (blog.example.com) |
Subfolder (example.com/blog) |
|
|---|---|---|
| Server | Can be a different one | Same host |
| DNS | Own record | No extra record |
| TLS certificate | Must cover the name | Already covered by the domain's |
| Cookies | Shared only if the cookie sets the parent domain | Shared by default |
| Best for | API, dashboard, staging, third-party service | Content of the same site |
Use a subdomain when that part of the project runs elsewhere, uses another stack or needs isolation. Use a subfolder when it is content of the same site and you want everything under one host.
When is the "subdomain" not yours?#
com, co.uk and github.io are public suffixes: the Public Suffix List (publicsuffix.org) records where the "registrable domain" ends. That is why example.com is a domain you own, and co.uk is not. Browsers use this list to stop one site from setting cookies for every other site under the same suffix.
When a service hands you myapp.provider.com, that is a subdomain of the provider, not yours. You do not control its DNS, and to get a name of your own you must point a domain you own at it. See how to use a custom domain and how to point a domain to your app.
How to do this on Square Cloud#
Every web application on Square Cloud gets a *.squareweb.app subdomain with HTTPS, on every plan. It works for testing and for receiving webhooks (Telegram, for example, requires HTTPS).
To use api.example.com, you need a custom domain, available starting with the Standard plan. At your registrar, you create the DNS records the dashboard shows and follow the validation status in real time. TLS is automatic. The domain stays registered in your name at your registrar, and the DNS zone stays under your control, at your DNS provider.
If one domain must be served by more than one application, use load balancing on the custom domain, with round-robin and passive failover. See the Domains and Load balancing pages for details.
Network
Custom domains, automatic TLS and a global CDN for your app
FAQs
Do subdomains cost money?
Usually not. You register the main domain and create as many subdomains as you like in your DNS panel, with no per-subdomain fee. The charge is for registering the domain, not for names created under it.
What is the difference between a subdomain and a subfolder?
A subdomain is blog.example.com and can point to another server. A subfolder is example.com/blog and lives on the same host. To DNS they are different things: only the subdomain has its own records.
Can I use a CNAME on the root domain?
Not under the DNS standard: a CNAME cannot coexist with other records at the same name, and the root domain always has NS and SOA records. Use an A or AAAA record, or ALIAS/ANAME if your DNS provider offers it.
How many levels of subdomain can I have?
Each label can be up to 63 characters and the full name up to 253 characters in text form. Within that, you can chain levels, like v2.api.example.com.
More in Networking and domains
See all 27 articles- What is a load balancer and how does it work?A load balancer spreads incoming requests across several servers. Learn how it works, the main algorithms, layer 4 vs 7, and a working nginx example.
- DNS records explained: A, CNAME, MX, TXTDNS records are the instructions in a zone that tell the internet where a name points. Learn A, AAAA, CNAME, MX, TXT, NS and CAA with examples and dig commands.
- How to point a domain to your server or appTo point a domain to your server or app, create a CNAME (or A) record in your registrar's DNS panel. Steps, how to test it, and common mistakes.
- Cache-Control headers explainedCache-Control is the HTTP header that sets how long browsers and CDNs keep a response. Learn max-age, no-cache, immutable and ready-made recipes for each file type.