0

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).

Jonny
  • 103
  • 3

1 Answers1

1

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>
HBruijn
  • 77,029
  • 24
  • 135
  • 201