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

Is there a way in apache2 to add a virtual host for subdomains who don't exists?

Now i try this:

<VirtualHost *:80>
        ServerAdmin info@devhouse.nl

        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>

        ErrorLog /var/log/apache2/error.log


</VirtualHost>

<VirtualHost *:80>
        ServerAdmin info@devhouse.nl
        ServerName *.devhouse.nl
        DocumentRoot /var/www/errors
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/errors/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ErrorLog /var/log/apache2/error.log

</VirtualHost>

Include sites-available/devhouse.nl.vhost

(the second virtualhost)

But this don't work. The I see the file in /var/www not in /var/www/errors

How can i fix this?

share|improve this question

2 Answers 2

You can create a default VirtualHost, that is triggered whenever there's no matching NamedVirtualHost configured. This would basically look like the first example you gave. Then you add NamedVirtualHosts for all domains you want to handle (e.g. add ServerName www.example.com etc).

Whenever a virtual host is requested that's not configured, the default will be serving that request. On this you might want to either display specific content and/or trigger a redirect to an existing (or different) server.

You might need to declare NameVirtualHost *:80 prior to the VirtualHost blocks.

share|improve this answer

If NameVirtualHost *:80 or something similar isn't already in your config files, it will have to be. This is necessary when your VirtualHosts are using the same IP address and port (which yours are). This tells Apache that it should use the Host: header in the HTTP request to choose which VirtualHost should handle that request.

You must also have a ServerName directive in each VirtualHost if you are using NameVirtualHost. This is what Apache matches the Host: header to. The ServerName directive cannot contain the * character. The ServerName must match your domain exactly. If you want to match www.devhouse.nl as well as devhouse.nl then you will need a ServerAlias directive. You can have as many ServerAlias directives as you like but only one ServerName.

ServerName devhouse.nl
ServerAlias www.devhouse.nl
ServerAlias mail.devhouse.nl

A VirtualHost with the above in it would match an HTTP request that was sent to one of those three domains. Any other request will go to the default VirtualHost.

The first VirtualHost listed in the config files will become the default VirtualHost. This is the one that will handle a request if it doesn't match any other VirtualHost. If you want the one you have listed second to be the default, you will have to swap them around in the file.

The Apache documentation on this is quite good: http://httpd.apache.org/docs/2.2/vhosts/examples.html

You can have Apache list your VirtualHosts and what order they come in with the command apachectl -S (That's a capital S)

The Include at the end looks rather odd. Is the first part of the listing the contents of that included file ?

In case all of that is too much to take in at once, what you need to do here is:

  1. Swap the two VirtualHosts around.
  2. Put NameVirtualHost *:80 before both of them.
  3. Get rid of *. from the ServerName directive or replace it with www. It doesn't matter exactly what goes in the default VirtualHost as long as it is a domain you want to match.
  4. Add a ServerName directive and optionally some ServerAlias directives to the second VirtualHost.
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.