Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

If I have 3 domains, domain1.com, domain2.com, and domain3.com, is it possible to set up a default virtual host to domains not listed? For example, if I would have:

<VirtualHost 192.168.1.2 204.255.176.199>
DocumentRoot /www/docs/domain1
ServerName domain1
ServerAlias host
</VirtualHost>

<VirtualHost 192.168.1.2 204.255.176.199>
DocumentRoot /www/docs/domain2
ServerName domain2
ServerAlias host
</VirtualHost>

<VirtualHost 192.168.1.2 204.255.176.199>
DocumentRoot /www/docs/everythingelse
ServerName *
ServerAlias host
</VirtualHost>

If you register a domain and point it to my server, it would default to everythingelse showing the same as domain3. Is that possible?

share|improve this question

7 Answers 7

up vote 18 down vote accepted

Yes, that should work, except ServerAlias should be "*", with ServerName set to an actual hostname. You might need to make sure that VirtualHost is the very last loaded...

share|improve this answer
    
It should work, but doesn't. If a domain is not specifically listed, I get "Firefox can't find the server." –  SJaguar13 Nov 6 '09 at 22:22
1  
Did you set it as "ServerName host" and "ServerAlias *"? I didn't emphasize this enough originally, but ServerName does not take wildcards, only ServerAlias does. ServerName needs to be an actual hostname. –  freiheit Nov 6 '09 at 22:44
    
Also, do the other virtualhosts work? What version of apache? –  freiheit Nov 6 '09 at 22:46

When using name-based virtual hosts, the first virtual host configuration loaded will be the default (Source: Apache Wiki). For example, with the configuration below, otherwise unmatched domains will match with domain-one.com:

NameVirtualHost *:80

<VirtualHost *:80>
  ServerName domain-one.com
  # Other options and directives ..
</VirtualHost>

<VirtualHost *:80>
  ServerName domain-two.com
  # Other options and directives ..
</VirtualHost>

Many servers do not have a monolithic configuration file, but have several host-specific configuration files organized as follows:

/etc/apache2
|-- sites_available  (actual configuration files)
`-- sites_enabled    (symlinks to files in sites_available)

In this case, to make a particular virtual host configuration load first, rename the symlink to something which will be first when sorted, such as 00-default.


Some of the other answers are not quite correct. According to the Apache Wiki, not setting a ServerName in a virtual host is incorrect. If the host without a ServerName is not loaded first, Apache may never even use it, since the first host loaded would be the default.

Furthermore, while ServerAlias * will indeed match anything, it may also override other virtual hosts defined later. Maybe this approach would work if it's always the last virtual host to be defined (as in the configuration given in the question), but this means adding a new directive and changing the sort order instead of just changing the order as above.

share|improve this answer
    
+ 1 million internets to you Sir! It has to be first to be default. –  Ryan Jun 11 '12 at 15:14
    
Do you know which one comes first, httpd.conf or conf.d/xyz.conf? –  Esa Varemo Sep 16 '12 at 21:59
    
+ 2 million internets, thank you Sir. –  Ben Everard Aug 22 at 12:22

Don't specify a servername, and that becomes your default vhost..

<VirtualHost *:80>
ServerAdmin webmaster@localhost

DocumentRoot /var/www
<Directory />
	Options FollowSymLinks
	AllowOverride None
</Directory>
<Directory /var/www/>
	Options Indexes FollowSymLinks MultiViews
	AllowOverride None
	Order allow,deny
	allow from all
</Directory>
</VirtualHost>

Also be sure that you haven't specified a DocumentRoot in the main httpd.conf file, as that will take precedence over the vhosts.

share|improve this answer
    
I have that as the first virtual host listed, and I still get "Firefox can't find the server." –  SJaguar13 Nov 8 '09 at 6:18
    
I disagree. I had my first virtual host set without a ServerName, however, it seems to conflict with some virtual hosts, but not others. I solved the issue by adding a ServerName, but setting it to some random domain that's not on my server. Since it's the first virtual host, it's used as the default, but only matches when a domain not matched to any other ServerName is used. –  joshaidan Jul 9 '13 at 16:20

Order is important - move your vhost definition for everything else to the head of the list.

share|improve this answer

I suggest making this your first VirtualHost entry:

<VirtualHost *>
  ServerName somethingthatwillnevermatch.com
  DocumentRoot /www/docs/everythingelse
</VirtualHost>

I've had cases where I didn't specify a ServerName, and my default entry was conflicting with other VirtualHosts. Perhaps "ServerName *" will also do the trick, but I did not test this. Either way, you should be okay with "ServerName somethingthatwillnevermatch.com".

share|improve this answer

The best solution is to rename the site configuration file starting with a "1" so it will load first and that will be your default site.

Apache2 makes the first loaded vhost file as the default page.

share|improve this answer

Use the _default_ virtual host and place it first in httpd-vhosts.conf as specified in http://httpd.apache.org/docs/2.2/vhosts/examples.html

"Catching every request to any unspecified IP address and port, i.e., an address/port combination that is not used for any other virtual host [...] 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)."

Snippet from a live but obfuscated httpd-vhosts.conf which happens to lock all vhosts to port 80:

# Listen for virtual host requests on all IP addresses.
# This directive cannot be removed:
NameVirtualHost *:80

<VirtualHost _default_:80>
# This vhost catches client requests with host headers which have
# not been matched by ServerName or ServerAlias directives in other vhosts.
#
# We redirect all such requests to a particular named vhost:
    RewriteCond %{HTTP_HOST}    ^(.*)$
    RewriteRule ^(.*)$  http://my.site.of.choice [R=permanent,L]
</VirtualHost>

# Name based vhosts here:
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName  my.other.site
    ServerAlias my.other.site2 my.other.site3
</VirtualHost>

# more vhosts etc...

An in-depth explanation of the vhost matching process can be found here: http://httpd.apache.org/docs/2.2/vhosts/details.html

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.