I am trying to setup Virtual Hosts on Apache that react to the certain subdomains but any domain name

Such as


so any domain that points to the machine along with the subxdomain would work

ServerName web.* ...

So web.blahblah.com as well as web.mooose.com will work (both *.blahblah.com and *.mooose.com resolve to the same machine)


The folloging snippet should do the trick:

NameVirtualHost *:80
<VirtualHost *:80>
  ServerName subdomain
  ServerAlias subdomain.*
  # other directives for the particular vhost

See the documentation on ServerAlias and Name-based Virtual Host Support for details.


I'm not sure if you can use mass vhosts (apache2). i've never done something like this before. http://httpd.apache.org/docs/2.0/vhosts/mass.html

What exactly do you want to achieve with this solution?


This should not be hard at all.

By default Apache will respond to all traffic on whatever port you configure it to.

Simply set it to listen to port 80 on the ip that the addresses are assigned to.

Set up a Vhost with a different directory and host name for each named site, and these should take priority over the wild card.

This will resort in all named sites to go where they need to and anything else that the server receives will get the default page.

Just to make sure, I would put the named sites above the wildcard in httpd.conf


You cannot use wildcards with ServerName.

What you can do is use the default VirtualHost as a catch-all:

<VirtualHost *:80>
  # NEVER add a ServerName in this VirtualHost

As long as there is no VirtualHost for web.blahblah.com as well as web.mooose.com they will both work under that default VirtualHost.

You can also use rewrites to redirect requests to different URLs:

  RewriteCond %{HTTP_HOST}      ^web.moose.com$
  RewriteRule ^/(.*)$ http://other.moose.com/web/$1 [R,L]

Hope that helps.


  • Actually, You can use wildcards on ServerName or ServerAlias directives. – GetFree Nov 20 '09 at 18:13
  • 1
    Actually, you can use wildcards on ServerAlias but not on ServerName directives. – joschi Nov 24 '09 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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