Is there a way in apache2 to add a virtual host for subdomains who don't exists?
Now i try this:
<VirtualHost *:80>
ServerAdmin [email protected]
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 [email protected]
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?
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.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.
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:
NameVirtualHost *:80
before both of them.*.
from the ServerName directive or replace it withwww.
It doesn't matter exactly what goes in the default VirtualHost as long as it is a domain you want to match.