Apache is a popular web server used by millions of websites & blogs around the world. ServerName and ServerAlias are two important server directives for Apache, required by almost every website. Yet, it can be confusing to know when to use ServerName and when to use ServerAlias. In this article, we will look at the difference between ServerName and ServerAlias in Apache.
Difference Between ServerName and ServerAlias
Here are the important differences between ServerName and ServerAlias.
Also read : How to Install NFS Server and Client in CentOS
ServerName is used to identify the host name and port of your website/blog. In other words it is used to set the main address/domain name of your website.
Also read : How to Sort Dictionary by Value in Python
ServerAlias is used to identify the alternate host names for your website. For example, if you want the same server (virtual host) to listen to different hostnames or their variations, add them to ServerAlias.
ServerAlias www.example.com www1.example.com blog.example.com OR ServerAlias www.example.com ServerAlias www1.example.com ServerAlias blog.example.com
The thing is that Apache will try to exactly match the domain name present in requests with the ServerName of your virtual host. Even minor variations in the domain name need to be mentioned separately. For Apache, example.com and www.example.com are different host names.
Since there can be only one ServerName value, you need to specify the alternatives as ServerAlias.
Here are the key differences between ServerName and ServerAlias.
- ServerName can only contain 1 value while ServerAlias can contain multiple values.
- ServerName can accept port numbers while ServerAlias cannot
- ServerAlias can contain wildcard characters (e.g. *.example.com) while ServerName cannot
- ServerName is used to set the main address while ServerAlias is used to set alternative host names.
As you can see, both ServerName and ServerAlias have their own uses.
Also read : How to Install Flask in Ubuntu