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.

4 Answers 4

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.

8
  • * is fine - ServerAlias can accept wildcards. 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, 2009 at 21:10
  • I tried this configuration and all requests went to example.com. This is the output of apache2ctl -S <pre> 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) </pre> May 18, 2009 at 21:26
  • * is perfectly valid syntax.
    – ceejayoz
    May 19, 2009 at 2:48
  • You're correct about the * , I fixed it - this is my lowest scored accepted answer so far
    – reconbot
    May 19, 2009 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! May 19, 2009 at 20:22
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.

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.

1
  • This is really cool - I had forgotten completely about this.
    – reconbot
    May 19, 2009 at 18:33
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.

2
  • 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, 2009 at 21:11
  • yeah figured they wouldn't effect things, site actually loads and works properly when it goes to the right one! May 18, 2009 at 21:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .