Apache HTTP Server Version 1.3Apache name-based Virtual Host SupportSee Also: IP-based Virtual Host SupportName-based vs. IP-based virtual hostsWhile the approach with IP-based virtual hosts works very well,
it is not the most elegant solution, because a dedicated IP address
is needed for every virtual host and it is hard to implement on some
machines. The The benefits of using the new name-based virtual host support is a practically unlimited number of servers, ease of configuration and use, and requires no additional hardware or software. The main disadvantage is that the client must support this part of the protocol. The latest versions of most browsers do, but there are still old browsers in use who do not. This can cause problems, although a possible solution is addressed below. Using non-IP Virtual HostsUsing the new virtual hosts is quite easy, and superficially looks
like the old method. The notable difference between IP-based and
name-based virtual host configuration is the
For example, suppose that both www.domain.tld and
www.otherdomain.tld point at the IP address
111.22.33.44. Then you simply add to one of the Apache
configuration files (most likely
NameVirtualHost 111.22.33.44
<VirtualHost 111.22.33.44>
ServerName www.domain.tld
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost 111.22.33.44>
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain
</VirtualHost>
Of course, any additional directives can (and should) be placed
into the Note: When you specify an IP address in a Additionally, many servers may wish to be accessible by more than
one name. For example, the example server might want to be accessible
as
ServerAlias domain.tld *.domain.tld
Note that you can use ServerAlias if you are
serving local users who do not always include the domain name.
For example, if local users are
familiar with typing "www" or "www.foobar" then you will need to add
ServerAlias www www.foobar. It isn't possible for the
server to know what domain the client uses for their name resolution
because the client doesn't provide that information in the request.
The ServerAlias directive is generally a way to have different
hostnames pointing to the same virtual host.
Compatibility with Older BrowsersAs mentioned earlier, there are still some clients in use who do not send the required data for the name-based virtual hosts to work properly. These clients will always be sent the pages from the first virtual host listed for that IP address (the primary name-based virtual host). There is a possible workaround with the
Example configuration:
NameVirtualHost 111.22.33.44
<VirtualHost 111.22.33.44>
ServerName www.domain.tld
ServerPath /domain
DocumentRoot /web/domain
</VirtualHost>
What does this mean? It means that a request for any URI beginning
with "/domain" will be served from the virtual host
www.domain.tld This means that the pages can be accessed as
In order to make this work, put a link on your primary virtual host's page to http://www.domain.tld/domain/ Then, in the virtual host's pages, be sure to use either purely relative links (e.g., "file.html" or "../icons/image.gif" or links containing the prefacing /domain/ (e.g., "http://www.domain.tld/domain/misc/file.html" or "/domain/misc/file.html"). This requires a bit of discipline, but adherence to these guidelines will, for the most part, ensure that your pages will work with all browsers, new and old. See also: ServerPath configuration example Apache HTTP Server Version 1.3 |