ThisCalculator

Everyday

Apache Virtual Host: Does It Catch Subdomains?

Updated · Standard formula, shown below

Average pace 5.00 min/km
Average speed
12.00 km/h
Result
Finish time
50 min
You entered
Distance
10 km
You entered

Apache Virtual Host: Does It Catch Subdomains?

No, an Apache virtual host does not automatically catch subdomains or the root domain unless you configure it to. A virtual host matches only the exact domain name (or wildcard pattern) you specify, so example.com, www.example.com, and sub.example.com each need their own ServerName or ServerAlias entry to be handled correctly. This article explains how virtual host matching works, how to set it up for multiple subdomains, and common pitfalls.

Formula

Average pace (min/km) = Finish time (minutes) ÷ Distance (km)

Average speed (km/h) = Distance (km) ÷ (Finish time (minutes) ÷ 60)

Where:

  • t = finish time in minutes
  • d = distance in kilometers
  • pace = minutes per kilometer (lower is faster)
  • speed = kilometers per hour (higher is faster)

Worked Example

Using the default values: t = 50 minutes, d = 10 km.

  1. Pace = 50 ÷ 10 = 5.00 min/km
  2. Speed = 10 ÷ (50 ÷ 60) = 10 ÷ 0.8333 = 12.00 km/h

So a 50‑minute, 10‑km run gives an average pace of 5:00 per kilometer and an average speed of 12 km/h.

How Virtual Host Matching Works

Apache uses the ServerName and ServerAlias directives to decide which virtual host answers a request. When a request arrives, Apache compares the Host header (the domain name the browser sends) against the ServerName and ServerAlias of each virtual host. The first match in the configuration order wins. If no match is found, the first virtual host defined (often called the default vhost) handles the request.

This means that a virtual host with ServerName example.com will only respond to example.com – not to www.example.com or sub.example.com unless you add them as aliases or use a wildcard.

Setting Up Subdomains and the Root Domain

To catch both the root domain and all subdomains, you have two main approaches:

  • Explicit listing: Add every subdomain as a ServerAlias:
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com sub.example.com
    DocumentRoot /var/www/html
</VirtualHost>
  • Wildcard: Use a wildcard in ServerAlias to match any subdomain:
<VirtualHost *:80>
    ServerName example.com
    ServerAlias *.example.com
    DocumentRoot /var/www/html
</VirtualHost>

The wildcard *.example.com matches www.example.com, sub.example.com, anything.example.com, and so on. However, it does not match the bare root domain example.com – that’s why you still need ServerName example.com.

How to Choose the Right Configuration

Your choice depends on what you need:

  • One site for everything: Use a wildcard alias (*.example.com) plus ServerName example.com. All subdomains and the root point to the same content.
  • Different content per subdomain: Create separate <VirtualHost> blocks for each subdomain, each with its own ServerName and DocumentRoot. Do not use a wildcard in this case, or it will override your specific entries.
  • Only www and root: Add ServerAlias www.example.com to the root domain’s vhost.

Common Problems and Misconceptions

  • Wildcard does not match the root: As noted, *.example.com does not include example.com. You must explicitly set ServerName example.com.
  • First vhost is the default: If no vhost matches, Apache uses the first one defined. This can cause unexpected behavior if you have multiple vhosts – make sure the first one is a sensible default (e.g., a maintenance page).
  • Order matters: If you have both a wildcard vhost and a specific subdomain vhost, the specific one must come first in the configuration file; otherwise the wildcard will catch the request.
  • DNS must point to the server: Apache can only handle requests that reach the server. You must create DNS A/AAAA records for each subdomain you want to use, otherwise they won’t resolve to your server at all.

Comparison: Wildcard vs. Explicit Subdomains

  • Wildcard (*.example.com): Easy to set up, catches all current and future subdomains, but you cannot serve different content per subdomain without extra logic (e.g., using environment variables).
  • Explicit subdomains: More control, allows different document roots, SSL certificates, and configurations per subdomain, but requires editing the config each time you add a new subdomain.

Security and Performance Considerations

When using wildcards, be aware that any subdomain that resolves to your server will be handled by that vhost. If you don’t want unknown subdomains to be served, avoid wildcards and list only the ones you need. Also, if you use HTTPS, you need a certificate that covers all subdomains – a wildcard certificate (*.example.com) covers subdomains but not the root; you may need a certificate with both example.com and *.example.com in the SAN.

Frequently Asked Questions

Does Apache virtual host catch subdomains by default?

No. By default, a virtual host only matches the exact ServerName. Subdomains are not caught unless you add them as ServerAlias or use a wildcard.

Can I use a wildcard to catch both the root domain and subdomains?

Yes, but you need both ServerName example.com and ServerAlias *.example.com in the same vhost. The wildcard alone does not match the bare root.

What happens if a subdomain doesn’t match any virtual host?

Apache uses the first virtual host defined in the configuration file. This is often the default vhost, which may serve the wrong content. To avoid this, set up a default vhost that returns a 404 or a maintenance page.

Do I need separate virtual hosts for www and non-www?

Not necessarily. You can put both in one vhost by adding ServerAlias www.example.com. If you want to redirect one to the other, you can create a separate vhost that issues a redirect.

Does the order of virtual hosts matter?

Yes. Apache uses the first matching vhost, so if you have a wildcard vhost, it should come after any specific subdomain vhosts you want to take precedence.

Can I use a wildcard in ServerName instead of ServerAlias?

Yes, you can use ServerName *.example.com, but it’s more common to use ServerName example.com and put the wildcard in ServerAlias. Both work, but the latter is clearer.

Related

Formula used
in t | Finish time | min | 50 | 0
in d | Distance | km | 10 | 0
out pace | Average pace | min/km | t / d | 2
out speed | Average speed | km/h | d / (t / 60) | 2

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.