0

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> 

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

This solved it! I just added that line at the top of my "testing.conf" file.
This solves the simplest case when you just want to set up default DocumentRoot (which gets overridden by vhosts's DocumentRoot directive). But if you want to use other cumulative features (access control, proxying, rewriting...) you will probably get in trouble.
@PavelHoral Will your suggestion of starting them with numbers fix those issues?
VirtualHost encapsulates contained configuration so that it is used only when the vhost is activated. So yes, using the order of vhosts to define defaults (instead of introducing global directives) will fix that.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.