How do ServerName and ServerAlias work in Apache?

Better Stack Team
Updated on November 9, 2023

In Apache, ServerName and ServerAlias directives are used in the server configuration to define how the server should respond to different hostnames or domain names. These directives are essential for virtual host configurations, allowing the server to handle multiple websites or domains on a single server.

ServerName Directive:

  • ServerName specifies the main hostname or domain name associated with the server or a particular virtual host.
  • It's used to identify the default host when the server receives requests that don't match any ServerName or ServerAlias in the virtual host configuration.
  • Example:

     
    ServerName example.com
    
    

ServerAlias Directive:

  • ServerAlias allows the server to respond to additional hostnames besides the one specified in ServerName.
  • It's used to specify alternative domain names, subdomains, or aliases for the same website hosted on the server.
  • Example:

     
    ServerName example.com
    ServerAlias www.example.com blog.example.com
    
    

How They Work Together:

  • When a request comes to the server, Apache checks the ServerName and ServerAlias directives in the virtual host configurations to determine which configuration to use.
  • If the requested hostname matches the ServerName or any of the ServerAlias values in a virtual host block, Apache will use that virtual host configuration to serve the request.
  • If the request doesn't match any ServerName or ServerAlias in the virtual host configurations, Apache will use the default virtual host (the one defined first in the configuration) or the one associated with the main server.

Use Cases:

  1. Primary Domain: ServerName defines the main domain associated with a virtual host.
  2. Subdomains and Aliases: ServerAlias is used to specify additional domain names or subdomains pointing to the same virtual host.
  3. Wildcard Aliases: It's possible to use wildcards in ServerAlias to match multiple subdomains, for instance:

     
    ServerName example.com
    ServerAlias *.example.com
    
    

    This configuration would match any subdomain of example.com.

Virtual Host Example:

 
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com blog.example.com
    # ...other configuration directives for this host...
</VirtualHost>

In a multi-domain hosting setup, using ServerName and ServerAlias directives correctly is crucial to ensure that Apache serves the correct content for different domains and subdomains hosted on the same server. These directives help Apache determine which virtual host configuration to use based on the incoming request's hostname.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github