Don Rhummy
Don Rhummy

Reputation: 25900

With multiple websites hosted at same ip in Apache, how do I set which one is the default?

I have multiple VirtualHosts in Apache, all listening on port 80 with their ServerNames set to different web addresses. When I go directly to the ip address, Apache sends me to one of those websites as default. I want to change which virtualhost handles requests to the ip address. How would I do this?

<!-- This is currently the default (probably because it's first alphabetically) -->
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName mywebsite.com
    DocumentRoot /var/www/testingother
    ...
</VirtualHost>


<!-- I want this to be default -->
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName myotherwebsite.com
    DocumentRoot /var/www/testing
    ...
</VirtualHost>

Upvotes: 0

Views: 618

Answers (2)

jeroen
jeroen

Reputation: 91792

From my own apache configuration file:

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"

Note that this DocumentRoot is outside a VirtualHost block. I would assume you have something similar but if not, you can set it up like that. That would be easier then messing around with the order in which they appear.

Upvotes: 1

Pavel Horal
Pavel Horal

Reputation: 18224

Default virtual host is the first one to be loaded.

  • When in single file httpd.conf or vhosts.conf, just change the order of vhosts.
  • When in vhost file based config (e.g. Debian uses that), it is a convention to prefix vhost files with a number. Default is usually 000-default.

From Apache documentation:

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).

Upvotes: 0

Related Questions