I'm wondering if you can make use of the vhost_alias module along with a rewrite rule. So during my VirtualHost I've this:

VirtualDocumentRoot /var/www/%2+/%1

For instance, sub.mydomain.com points to /var/www/mydomain.com/sub/
But I wish to see if the folder is available to prevent obtaining a 404 error basically attempt to achieve a bogus subdomain.

And So I attempted to get this done

RewriteCond /var/www/%2+/%1 !-d
RewriteRule ^.*$ www.mydomain.com

However it does not work. Any ideas?

Found a different way to do things i wanted:

RewriteRule ^(.*)$ %{HTTP_HOST}/$1
RewriteRule ^(.*)\.([^/.]*)\.([^/.]*)\/(.*) /home/vhosts/$2.$3/$1$4 [E=VIRTUAL_ROOT:/home/vhosts/$2.$3/$1]

RewriteCond %{ENV:VIRTUAL_ROOT} !-d
RewriteRule .* http://www.mydomain.com [L]