Why does http://localhost redirect to my default virtual host once I setup virtual hosts in Apache?

http://httpd.apache.org/docs/1.3/vhosts/name-based.html

(Should be true for 2.x also)

"If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.

As a consequence, the first listed virtual host is the default virtual host. The DocumentRoot from the main server will never be used when an IP address matches the NameVirtualHost directive. If you would like to have a special configuration for requests that do not match any particular virtual host, simply put that configuration in a container and list it first in the configuration file."


answer 1 is correct and i'd add with namevirtualhosts as the first entry essentially matches any not-named elsewhere virtualhost

it should ONLY be used to catch unintentional mal-formed and broken traffic

ie a machene with one ip called john.domain.com running www.domain.com and www.domain2.com as valid webservers on ip www.xxx.yyy.zzz might have an optimal config like thus

    <VirtualHost *:80>
     DocumentRoot /var/webserver/static-sites/unknown/
    # a directory readable by apache with only a robots.txt denying everything
     ServerName bogus
     ErrorDocument 404 "/errordocuments/unknown-name.html"
    #custom 404 describing how/what they might have done wrong try pointing a browser {with a hosts file at http://bogus/ on 193.120.238.109 to see mine#
     ErrorLog /var/log/httpd/unknown-error.log
     CustomLog /var/log/httpd/unknown-access.log combined
    </VirtualHost>

    <VirtualHost *:80>
     DocumentRoot /var/webserver/static-sites/unknown/
    # a possibly different directory readable by apache with only a robots.txt denying everything
     ServerName www.xxx.yyy.zzz
     ServerAlias john.domain.com
     ErrorDocument 404 "/errordocuments/ip-name.html"
     ErrorDocument 403 "/errordocuments/ip-name.html"
    #custom 404 telling them as a likely hacker/bot you wish to have nothing to do with them see mine at http://193.120.238.109/
     ErrorLog /var/log/httpd/ip-error.log
     CustomLog /var/log/httpd/ip-access.log combined
    </VirtualHost>

    <VirtualHost *:80>
     ServerName domain.com
     RedirectPermanent / http://www.domain.com/
     ErrorLog logs/www.domain.com-error.log
     CustomLog logs/www.domain.com-access.log combined
    </VirtualHost>

    <VirtualHost *:80>
     DocumentRoot /var/webserver/ftpusers/domain
     ServerName www.domain.com
     ServerPath /domain
     ErrorLog logs/www.domain.com-error.log
     CustomLog logs/www.domain.com-access.log combined
    </VirtualHost>

    <VirtualHost *:80>
     ServerName domain2.com
     RedirectPermanent / http://www.domain2.com/
     ErrorLog logs/www.domain2.com-error.log
     CustomLog logs/www.domain2.com-access.log combined
    </VirtualHost>

    <VirtualHost *:80>
     DocumentRoot /var/webserver/ftpusers/domain2
     ServerName www.domain2.com
     ServerPath /domain2
     ErrorLog logs/www.domain2.com-error.log
     CustomLog logs/www.domain2.com-access.log combined
    </VirtualHost>

Confirming that for Apache 2.x, the first virtual host (with the same port number) will be used if a matching virtual host is not found.

http://httpd.apache.org/docs/2.2/vhosts/details.html

"If no matching vhost could be found the request is served from the first vhost with a matching port number that is on the list for the IP to which the client connected"

You can always add this code below, put it right below NameVirtualHost *:80 so that your default document root is served by default if no other virtual hosts found.

<VirtualHost *:80>
    ServerName localhost
    DocumentRoot /my/default/document/root
</VirtualHost>

Tags:

Apache

Http