When setting up a website, you'll often need to handle both the root domain (example.com
) and its www subdomain (www.example.com
). The DNS system treats these as distinct entities, which leads to the common question: Do we need separate records for each?
Many developers initially try using a wildcard record:
*.example.com 3600 A 0 192.1.2.3
While this catches all subdomains, it doesn't cover the root domain. Conversely, a root record:
example.com 3600 A 0 192.1.2.3
won't automatically include the www subdomain.
The most reliable approach is using two separate records:
example.com 3600 A 0 192.1.2.3
www.example.com 3600 A 0 192.1.2.3
This ensures both domain variations resolve correctly. However, there's a more maintainable alternative.
For better maintainability, consider:
example.com 3600 A 0 192.1.2.3
www.example.com 3600 CNAME example.com
This configuration has several advantages:
- Single IP address management (change only the A record)
- Follows common web hosting practices
- Reduces potential DNS propagation issues
Here's how this would look in a BIND zone file:
$ORIGIN example.com.
@ 3600 IN A 192.1.2.3
www 3600 IN CNAME @
The @
symbol represents the root domain, making the configuration more readable.
Remember to:
- Set proper TTL values (3600 seconds is common)
- Configure your web server to handle both domains
- Implement proper redirects (usually root → www or www → root)
- Consider using a CDN which might require different DNS setups
After making changes, verify with:
dig example.com A +short
dig www.example.com A +short
Both should return your server's IP address (or the CNAME should resolve correctly).
When setting up DNS records for a website, developers often encounter this dilemma: how to properly configure records to serve both www-prefixed and bare domain versions of the site. The root issue stems from how DNS wildcards work versus explicit records.
# Problem scenario 1 - Wildcard only
*.example.com 3600 A 0 192.1.2.3
# example.com won't resolve because wildcards don't cover the bare domain
# Problem scenario 2 - Bare domain only
example.com 3600 A 0 192.1.2.3
# www.example.com won't resolve without specific record
There is no single record type that can handle both cases simultaneously. You must use either:
# Option 1: Dual A records
example.com 3600 A 0 192.1.2.3
www.example.com 3600 A 0 192.1.2.3
# Option 2: A + CNAME (preferred)
example.com 3600 A 0 192.1.2.3
www.example.com 3600 CNAME example.com.
The CNAME method provides several technical advantages:
- Single point of IP address management (change only the A record)
- Proper alignment with HTTP redirect strategies
- Better DNS cache behavior
- Cleaner infrastructure as you scale
Here's how this would look in various DNS management systems:
# AWS Route 53 example
{
"ResourceRecordSets": [
{
"Name": "example.com",
"Type": "A",
"TTL": 300,
"ResourceRecords": [{"Value": "192.1.2.3"}]
},
{
"Name": "www.example.com",
"Type": "CNAME",
"TTL": 300,
"ResourceRecords": [{"Value": "example.com"}]
}
]
}
# DigitalOcean DNS example
; A Records
@ 3600 IN A 192.1.2.3
www 3600 IN CNAME example.com.
For production environments, consider these additional factors:
- Always set up corresponding HTTPS records (use Let's Encrypt's DNS-01 challenge)
- Implement proper TTL values (lower for staging, higher for production)
- Configure appropriate redirects at the web server level (301 from www to bare or vice versa)
- Set up DNSSEC where applicable