Is this the version you want? For more recent versions, check our documentation index.
_default_
vhostsServerPath
directiveServer configuration:
...
Port 80
ServerName server.domain.tld
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/domain
ServerName www.domain.tld
...
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/subdomain
ServerName www.sub.domain.tld
...
</VirtualHost>
The asterisks match all addresses, so the main server
serves no requests. Due to the fact that
www.domain.tld is first in the configuration
file, it has the highest priority and can be seen as the
default or primary server.
Server configuration:
...
Port 80
ServerName server.domain.tld
NameVirtualHost 111.22.33.44
<VirtualHost 111.22.33.44>
DocumentRoot /www/domain
ServerName www.domain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.44>
DocumentRoot /www/subdomain
ServerName www.sub.domain.tld
...
</VirtualHost>
Apart from localhost there are no
unspecified addresses/ports, therefore the main server
only serves localhost requests. Due to the
fact that www.domain.tld has the highest
priority it can be seen as the default or
primary server.
Server configuration:
... Port 80 ServerName www.domain.tld DocumentRoot /www/domain NameVirtualHost 111.22.33.55 <VirtualHost 111.22.33.55> DocumentRoot /www/otherdomain ServerName www.otherdomain.tld ... </VirtualHost> <VirtualHost 111.22.33.55> DocumentRoot /www/subdomain ServerName www.sub.domain.tld ServerAlias *.sub.domain.tld ... </VirtualHost>Any request to an address other than 111.22.33.55 will be served from the main server. A request to 111.22.33.55 with an unknown or noHost:header will be served from www.otherdomain.tld.
The server can be made to respond to internal and
external requests with the same content, with just one
VirtualHost section.
Server configuration:
...
NameVirtualHost 192.168.1.1
NameVirtualHost 111.22.33.55
<VirtualHost 192.168.1.1 111.22.33.55>
DocumentRoot /www/server1
ServerName server1.domain.tld
ServerAlias server1
...
</VirtualHost>
Now requests from both networks will be served from the
same VirtualHost
Server configuration:
...
NameVirtualHost 111.22.33.44:80
NameVirtualHost 111.22.33.44:8080
<VirtualHost 111.22.33.44:80>
ServerName www.domain.tld
DocumentRoot /www/domain-80
</VirtualHost>
<VirtualHost 111.22.33.44:8080>
ServerName www.domain.tld
DocumentRoot /www/domain-8080
</VirtualHost>
<VirtualHost 111.22.33.44:80>
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain-80
</VirtualHost>
<VirtualHost 111.22.33.44:8080>
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain-8080
</VirtualHost>
Server configuration:
...
Port 80
DocumentRoot /www/domain
ServerName www.domain.tld
<VirtualHost 111.22.33.55>
DocumentRoot /www/otherdomain
ServerName www.otherdomain.tld
...
</VirtualHost>
www.otherdomain.tld can only be reached
through the address 111.22.33.55, while
www.domain.tld can only be reached through
111.22.33.44 (which represents our main
server).
Server configuration:
...
Port 80
ServerName server.domain.tld
<VirtualHost 111.22.33.44>
DocumentRoot /www/domain
ServerName www.domain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.55>
DocumentRoot /www/otherdomain
ServerName www.otherdomain.tld
...
</VirtualHost>
The main server can never catch a request, because all IP
addresses of our machine are in use for IP-based virtual
hosts (only localhost requests can hit the
main server).
Server configuration:
...
Port 80
Listen 111.22.33.44:80
Listen 111.22.33.55:8080
ServerName server.domain.tld
<VirtualHost 111.22.33.44:80>
DocumentRoot /www/domain
ServerName www.domain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.55:8080>
ServerName www-cache.domain.tld
...
<Directory proxy:>
Order Deny,Allow
Deny from all
Allow from 111.22.33
</Directory>
</VirtualHost>
The main server can never catch a request, because all IP
addresses (apart from localhost) of our
machine are in use for IP-based virtual hosts. The web
server can only be reached on the first address through
port 80 and the proxy only on the second address through
port 8080.
Server configuration:
...
Port 80
ServerName server.domain.tld
NameVirtualHost 111.22.33.44
<VirtualHost 111.22.33.44>
DocumentRoot /www/domain
ServerName www.domain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.44>
DocumentRoot /www/subdomain1
ServerName www.sub1.domain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.44>
DocumentRoot /www/subdomain2
ServerName www.sub2.domain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.55>
DocumentRoot /www/otherdomain1
ServerName www.otherdomain1.tld
...
</VirtualHost>
<VirtualHost 111.22.33.66>
DocumentRoot /www/otherdomain2
ServerName www.otherdomain2.tld
...
</VirtualHost>
Server configuration:
...
Listen 80
Listen 8080
ServerName www.domain.tld
DocumentRoot /www/domain
<VirtualHost 111.22.33.44:8080>
DocumentRoot /www/domain2
...
</VirtualHost>
A request to www.domain.tld on port 80 is
served from the main server and a request to port 8080 is
served from the virtual host.
_default_
vhostsServer configuration:
... <VirtualHost _default_:*> DocumentRoot /www/default ... </VirtualHost>Using such a default vhost with a wildcard port effectively prevents any request going to the main server.
A default vhost never serves a request that was sent to an address/port that is used for name-based vhosts. If the request contained an unknown or noHost:header it is always served from the primary name-based vhost (the vhost for that address/port appearing first in the configuration file).
You can useAliasMatchorRewriteRuleto rewrite any request to a single information page (or script).
_default_ vhost for port 80.
Server configuration:
...
<VirtualHost _default_:80>
DocumentRoot /www/default80
...
</VirtualHost>
<VirtualHost _default_:*>
DocumentRoot /www/default
...
</VirtualHost>
The default vhost for port 80 (which must appear
before any default vhost with a wildcard port) catches
all requests that were sent to an unspecified IP address.
The main server is never used to serve a request.
Server configuration:
...
<VirtualHost _default_:80>
DocumentRoot /www/default
...
</VirtualHost>
A request to an unspecified address on port 80 is served
from the default vhost any other request to an
unspecified address and port is served from the main
server.
VirtualHost directive.
Server configuration:
...
Port 80
ServerName www.domain.tld
DocumentRoot /www/domain
NameVirtualHost 111.22.33.55
<VirtualHost 111.22.33.55 111.22.33.66>
DocumentRoot /www/otherdomain
ServerName www.otherdomain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.55>
DocumentRoot /www/subdomain
ServerName www.sub.domain.tld
ServerAlias *.sub.domain.tld
...
</VirtualHost>
The vhost can now be accessed through the new address (as
an IP-based vhost) and through the old address (as a
name-based vhost).
ServerPath directiveHost:
header. Old HTTP/1.0 clients do not send such a header and
Apache has no clue what vhost the client tried to reach
(and serves the request from the primary vhost). To provide
as much backward compatibility as possible we create a
primary vhost which returns a single page containing links
with an URL prefix to the name-based virtual hosts.
Server configuration:
... NameVirtualHost 111.22.33.44 <VirtualHost 111.22.33.44> # primary vhost DocumentRoot /www/subdomain RewriteEngine On RewriteRule ^/.* /www/subdomain/index.html ... </VirtualHost> <VirtualHost 111.22.33.44> DocumentRoot /www/subdomain/sub1 ServerName www.sub1.domain.tld ServerPath /sub1/ RewriteEngine On RewriteRule ^(/sub1/.*) /www/subdomain$1 ... </VirtualHost> <VirtualHost 111.22.33.44> DocumentRoot /www/subdomain/sub2 ServerName www.sub2.domain.tld ServerPath /sub2/ RewriteEngine On RewriteRule ^(/sub2/.*) /www/subdomain$1 ... </VirtualHost>Due to theServerPathdirective a request to the URL http://www.sub1.domain.tld/sub1/ is always served from the sub1-vhost.
A request to the URL http://www.sub1.domain.tld/ is only served from the sub1-vhost if the client sent a correctHost:header. If noHost:header is sent the client gets the information page from the primary host.
Please note that there is one oddity: A request to http://www.sub2.domain.tld/sub1/ is also served from the sub1-vhost if the client sent noHost:header.
TheRewriteRuledirectives are used to make sure that a client which sent a correctHost:header can use both URL variants, i.e., with or without URL prefix.