Section 4.7 of "Apache Cookbook" from Oreilly 2004 says:
Mass Virtual Hosting Using Rewrite Rules
Problem
Although there is a module - mod_vhost_alias - which is explicitly for the purpose of supporting large numbers of virtual hosts, it is very limiting and requires that every virtual host be configured exactly the same way. You want to support a large number of vhosts, configured dynamically, but, at the same time, you want to avoid mod_vhost_alias.
Solution
Use directives from mod_rewrite to map to a directory based on the hostname:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.com$
RewriteRule ^(.*)$ /home/%2/$1
References
http://httpd.apache.org/docs/vhosts/
http://httpd.apache.org/docs/mod/mod_rewrite.html
For your convience, here is Section 4.6 of the same book:
Mass Virtual Hosting with mod_vhost_alias
Problem
You want to host many virtual hosts, all of which have exactly the same configuration.
Solution
Use VirtualDocumentRoot and VirtualScriptAlias provided by mod_vhost_alias.
VirtualDocumentRoot /www/vhosts/%-1/%2.1/%2/htdocs
VirtualScriptAlias /www/vhosts/%-1/%-2.1/%-2/cgi-bin
example values for the hostname www.example.com
Value Meaning
%2 example
%3 com
%-1 com
%-2 example
%-3 www
%-2.1 e
%-2.2 x
%-2.3+ ample
References
http://httpd.apache.org/docs/mod/mod_vhost_alias.html
http://httpd.apache.org/docs/vhosts/