DNS CNAME records fundamentally cannot point to URL paths (like domain.com/subdir
) because DNS operates at the network layer while paths are HTTP/application-layer constructs. A CNAME can only map one domain name to another (e.g., blog.example.com
→ example.com
), not specific resources within a domain.
When examining DNS resolution:
dig CNAME example.com
;; ANSWER SECTION:
example.com. 3600 IN CNAME actual-server.com.
The resolution stops at the domain level. HTTP paths (/subdir
) are processed by web servers after DNS completes its resolution.
For those needing path-based routing:
- Web Server Config (Nginx example):
server { listen 80; server_name my.domain.com; location /subdir1 { proxy_pass http://backend-service; } }
- Cloud Solutions:
AWS CloudFront behaviors or Azure Front Door rules can route based on paths
Many developers confuse DNS with HTTP routing. Remember:
Layer | Handles | Doesn't Handle |
---|---|---|
DNS | Domain → IP | URL paths |
HTTP | /paths, ?queries | IP resolution |
A WordPress multisite setup often requires:
# DNS
blog CNAME → main-server.com
# .htaccess
RewriteRule ^blog/(.*)$ /wp-content/multisite/$1 [L]
This combines DNS (CNAME) with application-layer routing.
DNS records, including CNAME records, operate at the domain level and cannot directly reference URL paths or subdirectories. A CNAME record can only map one domain name (alias) to another domain name (canonical name), not to specific paths within that domain.
The DNS system resolves hostnames to IP addresses, not URLs. When you create:
sub.example.com. IN CNAME main.example.com/subdir
This configuration is invalid because:
- DNS protocol doesn't parse path components (/subdir)
- CNAME values must be valid hostnames without special characters
To achieve similar functionality, consider these alternatives:
1. Web Server Configuration
Configure your web server (e.g., Nginx, Apache) to handle redirects:
# Nginx configuration
server {
listen 80;
server_name sub.example.com;
location / {
proxy_pass http://main.example.com/subdir;
}
}
2. URL Rewriting
Use rewrite rules in your application framework:
// Express.js middleware example
app.use('/sub', (req, res) => {
res.redirect(301, 'https://main.example.com/subdir' + req.path);
});
Common use cases where developers attempt this:
- Microservices architecture with path-based routing
- Legacy system integration
- Multi-tenant SaaS applications
Instead of trying to make DNS handle path routing:
- Use proper subdomains for different services
- Implement API gateways for path-based routing
- Consider cloud provider solutions (AWS ALB, Cloudflare Workers)
Here's how to properly set up a subdomain that serves content from a subdirectory:
# DNS configuration (valid)
blog.example.com. IN CNAME hostingprovider.com.
# Web server configuration
server {
server_name blog.example.com;
root /var/www/main-site/blog;
# Additional configuration...
}