7

I want to map www.example.com to a specific virtual host and then I want all other subdomains of example.com to go to another virtual host.

To do this I created these hosts:

<VirtualHost *:80>
  ServerName www.example.com
</VirtualHost>

<VirtualHost *:80>
  ServerName example.com
  ServerAlias *.example.com
</VirtualHost>

Now the selection of which host is served seems rather random. If I restart apache sometimes I will get one host and other times another.

What am I doing wrong?

Thanks!

Update: If I run apache2ctl -S on this configuration I get this outpu:

VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server www.example.com (/etc/apache2/sites-enabled/dev:3)
         port 80 namevhost www.example.com (/etc/apache2/sites-enabled/dev:3)
         port 80 namevhost example.com (/etc/apache2/sites-enabled/dev:22)

After much digging around I decided to disable the mono applications that I had running and low and behold it started serving files from the correct site. They did need to be entered in the order:

<VirtualHost *:80>
  ServerName example.com
</VirtualHost>

<VirtualHost *:80>
  ServerName www.example.com
</VirtualHost>

as suggested by Wizard. To get my mono apps working I used "MonoAutoApplication enabled" within each virtual host. However I am not sure that this is the best option as on the mono site it says you shouldn't use auto hosting for asp.net mvc apps (which is what I am using). So far I can't see the downside though.

Derek Ekins
  • 223
  • 3
  • 8

4 Answers4

5

The code should probably be:

<VirtualHost _default_:80>
   ServerName example.com
</VirtualHost>

<VirtualHost *:80>
    ServerName www.example.com
</VirtualHost>

See: http://httpd.apache.org/docs/2.2/vhosts/examples.html#default

This explicitly defines the "example.com" vhost as the host to use if nothing else matches.

sysadmin1138
  • 133,124
  • 18
  • 176
  • 300
4

Are you sure that the www.example.com vhost appears before the other vhost in the configuration file? Apache is supposed to go through the virtual hosts in order and pick the first one with a ServerName or ServerAlias that matches the value sent in the Host HTTP header. It seems very odd that it would randomly pick sometimes one virtual host and sometimes the other.

It might help to edit your question to include the output of

apache2ctl -S

which shows the defined virtual hosts as Apache sees them.

David Z
  • 5,475
  • 2
  • 25
  • 22
3

Update: * is valid syntax but not necessary. You can find out more here.

This will work though.

<VirtualHost *:80>
   ServerName example.com
</VirtualHost>

<VirtualHost *:80>
    ServerName www.example.com
</VirtualHost>

The first directive will match everything that is not explicitly defined elsewhere.

reconbot
  • 2,455
  • 3
  • 25
  • 30
  • * is fine - ServerAlias can accept wildcards. http://httpd.apache.org/docs/2.2/mod/core.html#serveralias Although the configuration you've got may be the best solution, if the same server isn't hosting any other domain names. – David Z May 18 '09 at 21:10
  • I tried this configuration and all requests went to example.com. This is the output of apache2ctl -S
    VirtualHost configuration:
    wildcard NameVirtualHosts and _default_ servers:
    *:80                   is a NameVirtualHost
             default server example.com (/etc/apache2/sites-enabled/dev:3)
             port 80 namevhost example.com (/etc/apache2/sites-enabled/dev:3)
             port 80 namevhost www.example.com (/etc/apache2/sites-enabled/dev:24)
    
    – Derek Ekins May 18 '09 at 21:26
  • * is perfectly valid syntax. – ceejayoz May 19 '09 at 02:48
  • You're correct about the * , I fixed it - this is my lowest scored accepted answer so far – reconbot May 19 '09 at 18:19
  • adding the entries in that order did the trick - along with changing the way mod_mono was configured. I would vote you up but I don't have the rep! – Derek Ekins May 19 '09 at 20:22
  • I wonder if there's a badge lurking down there if we down-vote this a bit more? ^^ – Oskar Duveborn May 19 '09 at 20:40
  • That order works but I think the "" might be the better way to go that way you don't have to worry about moving stuff around. Check out sysadmin1138 post below. But if you do get rep, by all means mod me up ;-) – reconbot May 20 '09 at 17:00
  • I am not sure about the _defaut_ option as I do want to host other sites on this server and it seems a little strange to make this site the default for all requests to the server. But you are right, having to have this in the right order is kinda wrong. – Derek Ekins May 20 '09 at 22:34
1

You need the DocumentRoot added to the configuration. It should be something like this:

# Ensure that Apache listens on port 80
Listen 80

# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80

<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example1.com

# Other directives here

</VirtualHost>

<VirtualHost *:80>
DocumentRoot /www/example2
ServerName www.example2.org

# Other directives here

</VirtualHost>

EDIT: Seems I thought I knew more about Apache than I do. Thanks for the comments.

Joshua Nurczyk
  • 748
  • 6
  • 17
  • I think the OP would be experiencing different symptoms if the issue were a missing DocumentRoot (Apache warns about that sort of thing on startup, IIRC) – David Z May 18 '09 at 21:11
  • yeah figured they wouldn't effect things, site actually loads and works properly when it goes to the right one! – Derek Ekins May 18 '09 at 21:27