How can I set a default VirtualHost in Apache?
Preferably, I want the default host not to be the same as the IP address host. Now I have something like this:
NameVirtualHost * <VirtualHost *> ServerAdmin [email protected] DocumentRoot /someOtherDir/ ServerAlias ip.of.the.server </VirtualHost> <VirtualHost *> ServerAdmin [email protected] DocumentRoot /someroot/ ServerAlias domain.com *.domain.com </VirtualHost *>
If a domain is forwarded to my server, but isn't in this vhost.conf file, the files from /someOtherDir/ are loaded, as expected. But I want to be able to use a different root for the IP address itself and domains which aren't added to the vhost.conf file (yet). Is this possible?
The Apache HTTP server supports virtual hosts, meaning that it can respond to requests that are directed to multiple IP addresses or host names that correspond to the same host machine. You can configure each virtual host to provide different content and to behave differently.
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 no Host: header it is always served from the primary name-based vhost (the vhost for that address/port appearing first in the configuration file).
The solution is easy, because we can simply add the new IP address ( 172.20. 30.50 ) to the VirtualHost directive. 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).
<Virtualhost 10.0. 0.2:*> means "I'll use this virtualhost for request coming on my 10.0. 0.2 interface" <Virtualhost _default_:443> means "I'll use this virtualhost for all other network interface on my host for request coming on port 443"
I found the answer: I remembered that Apache uses the first block if no other matching block is found, so I've added a block without a serveralias
at the top of the blocks:
NameVirtualHost * <VirtualHost *> DocumentRoot /defaultdir/ </VirtualHost> <VirtualHost *> ServerAdmin [email protected] DocumentRoot /someOtherDir/ ServerAlias ip.of.the.server </VirtualHost> <VirtualHost *> ServerAdmin [email protected] DocumentRoot /someroot/ ServerAlias domain.com *.domain.com </VirtualHost>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With