I'm setting up named virtual host configurations for a domain e.g. www.examplesite.uk
Which of the below name & alias definitions would be preferable? Is there a difference? I've seen various examples online where it's both ways round.
ServerName www.examplesite.uk
ServerAlias examplesite.uk
or
ServerName examplesite.uk
ServerAlias www.examplesite.uk
or just
ServerName www.examplesite.uk
Thank you.
The difference is subtle. As described in Documentation
ServerNameshows the hostname and port the server uses to identify itself whileServerAliasgives alternate names for a host used when matching requests to name-virtual hosts.In name-based virtual hosts the hostname is specified by the request's Host header.
Using http://www.example.com to open a website results in 'www.example.com' as Host header. Using http://example.com results in 'exmaple.com' as Host header.
Hence, to cover both ways the common practice is:
If you put
ServerName www.examplesite.ukand Host header is 'examplesite.uk' Apache will check for matches with other VHs if there are any. If none Apache will use the first VH. So, your last option might not retrieve the webpage you want.You can also check: What is the difference between ServerName and ServerAlias in apache2 configuration?