Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk8 min

How to Set the Default Virtual Host in Apache 2.4

Apache 2.4 uses the first matching virtual host as the fallback for each address-and-port set. This guide shows how to order vhosts, configure names and DNS, compare _default_, and verify the effective default with apachectl -S.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Apache HTTP Server 2.4, the default virtual host is normally the first virtual-host block Apache loads for a particular address-and-port combination. Put the site you want as the fallback first, give it an explicit ServerName and DocumentRoot, place the other sites after it, and confirm Apache’s parsed order with apachectl -S.

What “default virtual host” means in Apache

Apache does not choose one universal default site for every connection. It builds separate virtual-host sets for each address and port. A request is first assigned to the most specific set that matches its destination address and connection port. Apache then compares the request’s host name with the ServerName and ServerAlias directives in that set.

As an Amazon Associate I earn from qualifying purchases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If a name matches, Apache serves the first virtual host in configuration order that declares that name. If no name matches, the first virtual host in the selected set becomes the fallback. This is why an unknown domain, a missing Host value, or a host name with no matching alias often opens the document root from the first block.

The address-and-port sets Apache considers

Apache evaluates virtual-host declarations in this precedence order:

  1. Exact IP address and exact port
  2. Exact IP address with a wildcard port
  3. Wildcard IP address with an exact port, such as *:80
  4. Wildcard IP address with a wildcard port
  5. The main server configuration when no virtual-host declaration matches

Only after Apache selects the most specific address-and-port set does it perform name matching. A first-listed block on *:80 is therefore the default for that set, not necessarily for a different address or port.

How the Host header is used

For HTTP requests, Apache compares the value in the Host header with ServerName and ServerAlias. The port written inside the Host header is ignored for this comparison; Apache uses the actual connection port to select the address-and-port set. Consequently, changing a host header from example.com to example.com:8080 does not make a port-8080 virtual host handle a connection that actually arrived on another port.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The recommended configuration

For ordinary name-based hosting on port 80, put the intended fallback first:

Listen 80

<VirtualHost *:80>
    # This first-listed virtual host is also the default for *:80
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /www/domain
</VirtualHost>

<VirtualHost *:80>
    ServerName other.example.com
    DocumentRoot /www/otherdomain
</VirtualHost>

In this example, a request whose Host is other.example.com goes to /www/otherdomain. Requests for www.example.com, example.com, an unknown host name, or a request without a usable host name fall back to the first block and its /www/domain document root.

Why explicit directives matter

  • ServerName: declares the primary host name for the site and makes matching predictable.
  • ServerAlias: adds alternate names, including wildcard aliases where appropriate.
  • DocumentRoot: identifies the files Apache serves for that virtual host.
  • Listen: ensures Apache is listening on the port represented by the virtual-host declaration.

Every name-based virtual host should declare its own ServerName. Relying on an inherited or guessed name makes it harder to understand which block should answer a request and can produce surprising fallbacks.

How to make one site the default, step by step

  1. Identify the exact address and port. Decide whether the fallback is for *:80, a specific IP and port, or another set. Default selection is calculated independently for each set.
  2. Find every virtual host in that set. Check all included Apache configuration files, not just the file you normally edit. A distribution may load site files from an include directory, and the effective order is the order Apache parses them.
  3. Edit the block that should be the fallback. Give it an explicit ServerName, any required ServerAlias values, and the intended DocumentRoot.
  4. Move that block before its peers. It must be the first listed <VirtualHost> block matching the same address and port. Merely changing the name does not change default priority.
  5. Keep the other sites after it. Each additional block should have its own ServerName and, when needed, ServerAlias. Do not place an unrelated block earlier in the same address-and-port set.
  6. Make DNS point to this server. DNS records for every public name must resolve to an address associated with the machine. An Apache directive does not create or change DNS.
  7. Inspect Apache’s parsed result. Run apachectl -S, or the equivalent control command supplied by your distribution. Read the reported default server, address, port, source file, and line order before testing from a browser.

The order shown by apachectl -S is authoritative for what the running configuration will use. If it differs from the order you expected, an include file, duplicate declaration, or disabled site file may be affecting the result.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Understanding requests that do not match a name

Unknown host names

Suppose the selected set contains two blocks, with www.example.com first and other.example.com second. A request for random.example.net does not match either ServerName or ServerAlias. Apache serves the first block, so the request receives the first site’s document root.

Missing or unusable Host values

When no usable host name is available, Apache still uses the first virtual host in the selected address-and-port set. This behavior is useful for a deliberate catch-all site, but it can expose the wrong application if the first block was ordered accidentally.

Aliases and wildcard names

ServerAlias extends the names that a block can match. Put all intended alternate names on the correct block, and use wildcard aliases only when they are genuinely meant to cover a family of host names. A name listed in a different block does not become an alias for the first site.

Address and port precedence in practice

Consider these declarations:

<VirtualHost 192.0.2.10:80>
    ServerName exact.example.com
</VirtualHost>

<VirtualHost *:80>
    ServerName wildcard.example.com
</VirtualHost>

A connection to 192.0.2.10 on port 80 is eligible for the exact-address set before the wildcard-address set. The first block in the exact set therefore controls unmatched names for that connection. A connection arriving on another address may instead use the *:80 set and its own first-listed fallback.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The same principle applies when you configure more than one port: the first vhost for *:80 is not automatically the first vhost for *:443 or another port. Each set needs an intentional order and explicit names.

Normal first-listed vhost versus _default_

Apache also supports a _default_ virtual-host address, but it solves a narrower problem and does not replace the normal first-listed fallback in a name-based set.

Characteristic First-listed <VirtualHost *:80> <VirtualHost _default_:80>
Address coverage Wildcard address for the declared port Requests sent to an unspecified address on the declared port
Port coverage The exact port in the declaration, such as 80 The exact port in the declaration, such as 80
Priority relative to * Higher precedence than _default_ when the wildcard set matches Lower precedence than any matching * declaration
Unknown Host in a name-based wildcard set Uses the first vhost in that set Does not displace that first vhost
Typical purpose Ordinary default or catch-all site for name-based hosting Handling an unspecified destination address when no higher-precedence wildcard set applies

Because any * virtual-host declaration has higher precedence than _default_, adding a _default_ block will not divert unknown host names away from a matching *:80 set. If your goal is to control the ordinary fallback for name-based hosting, reorder the first block in that set instead.

Verifying the effective default with apachectl -S

Configuration files can be misleading when several files are included. The command below asks Apache to dump its parsed virtual-host map:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
apachectl -S

Use the output to check:

  • Which virtual host Apache labels as the default server for each address and port
  • The address and port associated with each block
  • The order in which matching blocks were parsed
  • The source file and line number for every declaration

If your distribution uses a different control command name, use that equivalent command with the same virtual-host dump option. A browser test alone cannot tell you whether DNS, an HTTP proxy, or Apache’s own ordering caused the result; the configuration dump gives you the server-side answer first.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshooting the wrong default site

The intended block is not listed as the default

Check that it is the first block for the exact same address-and-port combination. A block that is first in one file may be parsed after another included file. Use the source file and line information from apachectl -S to locate the declaration Apache actually sees.

The right block is first, but the browser shows another site

Confirm that the request reaches the address and port you inspected. DNS may resolve the name to a different server or to an address with its own virtual-host set. Verify the requested name resolves to an address associated with this Apache instance, then compare that destination with the address shown by apachectl -S.

A named site is falling back unexpectedly

Compare the exact host name sent by the client with the block’s ServerName and ServerAlias. Differences in spelling, a missing alias, or a name placed on another block will prevent a match. Remember that Apache uses the real connection port, not a port textually appended to the Host value, when selecting the virtual-host set.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

An unknown domain opens a real application

This is the expected result when that application is first in the selected set. To prevent unintended exposure, create a deliberately minimal fallback block and put it first, with a document root that contains only the response you want unknown names to receive. Keep named production sites after it and assign each one explicit names.

_default_ did not change the result

Check whether a wildcard declaration such as *:80 already matches the connection. Wildcard virtual hosts have higher precedence than _default_, so the first block in the wildcard set remains the fallback for unmatched names.

Designing a predictable multi-site configuration

Use one intentional fallback per address-and-port set. A clear arrangement usually has:

  • A first block with the catch-all or default document root
  • An explicit ServerName for that block, even when it mainly handles unknown names
  • Explicit ServerName values for every other site
  • ServerAlias entries only for names that should reach the same site
  • Consistent address and port declarations so you know which blocks compete for first position

Document the reason for the first block in a configuration comment. Future administrators can then distinguish a deliberate fallback from an accidentally earliest file.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Quick Recap

Bestseller No. 2
Bestseller No. 4
Bestseller No. 5

Final checklist

  • Have you identified the exact address-and-port set?
  • Is the desired fallback the first parsed virtual host in that set?
  • Does every site declare an explicit ServerName?
  • Are alternate names present in the correct ServerAlias directive?
  • Does DNS resolve the requested names to this server’s address?
  • Did you verify the result with apachectl -S?
  • Are you using _default_ only for an unspecified address case rather than to override a matching wildcard set?

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.