Wildcard dns, non-existent subdomains point to main website

domain-name-systemsubdomainwildcard

Before I start I'd like to say that I've already read relevant questions here on serverfault but I couldn't find a solution.

Here's my problem. I'm using a third party DNS and I've added a wildcard A record *.biglle.com which points to my server's ip address. The problem I'm having with that is that every non existent sub-domain points to biglle.com(those that do exist work fine). I'd like to note that the apache website configuration for biglle.com is not the default one (000-default.conf) but a separate one (biglle.com.conf). That's what I can't understand. The non-existent subdomains should point to the default configuration file, shouldn't they?

Here is part of biglle.com.conf

<VirtualHost 192.168.1.54:80>
SuexecUserGroup "#1003" "#1004"
ServerName biglle.com
ServerAlias www.biglle.com
ServerAlias webmail.biglle.com
ServerAlias admin.biglle.com
DocumentRoot /home/biglle/public_html
ErrorLog /var/log/virtualmin/biglle.com_error_log
CustomLog /var/log/virtualmin/biglle.com_access_log combined
ScriptAlias /cgi-bin/ /home/biglle/cgi-bin/
ScriptAlias /awstats/ /home/biglle/cgi-bin/
DirectoryIndex index.html index.htm index.php index.php4 index.php5
<Directory /home/biglle/public_html>
Options -Indexes +IncludesNOEXEC +FollowSymLinks +ExecCGI
allow from all
AllowOverride All
AddHandler fcgid-script .php
AddHandler fcgid-script .php5
FCGIWrapper /home/biglle/fcgi-bin/php5.fcgi .php
FCGIWrapper /home/biglle/fcgi-bin/php5.fcgi .php5
</Directory>
<Directory /home/biglle/cgi-bin>
allow from all
</Directory>
RewriteEngine on
RewriteCond %{HTTP_HOST} =webmail.biglle.com
RewriteRule ^(.*) https://biglle.com:20000/ [R]
RewriteCond %{HTTP_HOST} =admin.biglle.com
RewriteRule ^(.*) https://biglle.com:10000/ [R]
RemoveHandler .php
RemoveHandler .php5
IPCCommTimeout 31
FcgidMaxRequestLen 1073741824
<Files awstats.pl>
AuthName "biglle.com statistics"
AuthType Basic
AuthUserFile /home/biglle/.awstats-htpasswd
require valid-user
</Files>
Alias /dav /home/biglle/public_html
Alias /pipermail /var/lib/mailman/archives/public
<Location /dav>
DAV on
AuthType Basic
AuthName "biglle.com"
AuthUserFile /home/biglle/etc/dav.digest.passwd
Require valid-user
ForceType text/plain
Satisfy All
RemoveHandler .php
RemoveHandler .php5
RewriteEngine off
</Location>
RedirectMatch /cgi-bin/mailman/([^/\.]*)(.cgi)?(.*) https://biglle.com:10000/virtualmin-mailman/unauthenticated/$1.cgi$3
RedirectMatch /mailman/([^/\.]*)(.cgi)?(.*) https://biglle.com:10000/virtualmin-mailman/unauthenticated/$1.cgi$3
php_value memory_limit 32M
</VirtualHost>

Currently I'm using a php hack that checks if the domain name has a respective .conf file in /etc/apache2/sites-enabled/ and if it doesn't it displays a 404 error but that's obviously not a permanent solution. Also this hack only works for the index page of each subdomain.

Due to me not having more than 10 rep I can't post examples here. You can see them on this paste here:
http://pastebin.com/cYY3ffYz

EDIT: apache2ctl -S output

root@biglle:/# apache2ctl -S
[Sun May 27 22:00:11 2012] [warn] NameVirtualHost 91.132.57.179:80 has no VirtualHosts
VirtualHost configuration:
192.168.1.54:80        is a NameVirtualHost
         default server biglle.com (/etc/apache2/sites-enabled/biglle.com.conf:1)
         port 80 namevhost biglle.com (/etc/apache2/sites-enabled/biglle.com.conf:1)
         port 80 namevhost fwnh.biglle.com (/etc/apache2/sites-enabled/fwnh.biglle.com.conf:1)
         port 80 namevhost itemlist.biglle.com (/etc/apache2/sites-enabled/itemlist.biglle.com.conf:1)
         port 80 namevhost lithisdoma.gr (/etc/apache2/sites-enabled/lithisdoma.gr.conf:1)
         port 80 namevhost think.biglle.com (/etc/apache2/sites-enabled/think.biglle.com.conf:1)
         port 80 namevhost topirouni.biglle.com (/etc/apache2/sites-enabled/topirouni.biglle.com.conf:1)
         port 80 namevhost videos.biglle.com (/etc/apache2/sites-enabled/videos.biglle.com.conf:1)
192.168.1.54:443       is a NameVirtualHost
         default server biglle.com (/etc/apache2/sites-enabled/biglle.com.conf:58)
         port 443 namevhost biglle.com (/etc/apache2/sites-enabled/biglle.com.conf:58)
wildcard NameVirtualHosts and _default_ servers:
*:80                   biglle.com (/etc/apache2/sites-enabled/000-default:1)
Syntax OK

Best Answer

Apache will look for VirtualHosts in the following order:

  1. First defined VirtualHost with matching IP, port, and ServerName
  2. First defined VirtualHost with matching IP and port
  3. First defined VirtualHost with wildcard (or _default_) IP and matching port

I'm assuming that the server only has one IP address, namely 192.168.1.54.

192.168.1.54:80        is a NameVirtualHost
    default server biglle.com (/etc/apache2/sites-enabled/biglle.com.conf:1)
wildcard NameVirtualHosts and _default_ servers:
    *:80                   biglle.com (/etc/apache2/sites-enabled/000-default:1)

Incoming requests on 192.168.1.54:80 which don't match a ServerName will therefore use the biglle.com VirtualHost defined in biglle.com.conf (because that is the first VirtualHost which matches the IP and port). The biglle.com VirtualHost in 000-default will only be used for requests received on an IP other than 192.168.1.54.

Unless you're actually doing IP based virtual hosting, I suggest that you change all your VirtualHosts to not specify an IP address (i.e. <VirtualHost *:80>), and make sure that the one you want to be the default is listed first in the configuration (which the 000-default file should do).