Should a virtualhost always use the naked domain as the ServerName?

Posted on

Should a virtualhost always use the naked domain as the ServerName? – Problems with loading a website are often blamed on the Internet connection, but even the most perfectly set up network cannot help if there is no service to reply at your destination. One of the most popular HTTP servers used for this task is Apache2. Much of Apache’s popularity can be attributed to its easy installation and use, but never the less it is possible to run into problems with even the easiest of the software. If you’ve encountered an issue loading your web page, follow these simple troubleshooting methods outlined in this guide to attempt to get your web server back up and working again. Below are some tips in manage your apache2 server when you find problem about apache-2.2, virtualhost, domain, , .

When setting up a virtual host for example.com, and wanting to use www.example.com as my preferred domain, is it standard practice to use:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    # ...
</VirtualHost>

… wherein my preferred domain is an alias?

Does it matter if I were to switch the values of the ServerName and ServerAlias? As far as I can tell, they are interchangeable (with the exception that you can specify multiple server aliases).

As far as I know switching the values of the ServerName and ServerAlias normally makes no difference in how your sites will operate.

As mentioned in the manual only when UseCanonicalName is set to a non-default value, you’ll see the value of ServerName (rather than whatever URL was used to reach your site) getting used when creating self-referential redirection URL’s.
You’ll see self-referential redirection URL’s for instance if the users type a ServerAlias hostname and a URL which is a directory, such as http://www.example.com/splat, without the trailing slash, then Apache httpd will redirect them to http://example.com/splat/.

Otherwise you’ll only see the value of the ServerName directive in the output of apachectl -S and in error messages as the standard templates for friendly error messages include the ServerName from include/bottom.html to customise them:

Title: 404 Not Found

Not Found

The requested URL /some/page was not found on this server.


Apache/2.2.15 (CentOS) Server at example.com port 80


There are some valid technical arguments to use www.example.com and you may want to do this instead:

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / www.example.com 
</VirtualHost>
<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias www.example.org www.example.net 
    # ...
</VirtualHost>

Leave a Reply

Your email address will not be published.