- With vhost_alias_module enabled
<VirtualHost *:80> VirtualDocumentRoot c:/projects/%1/ </VirtualHost>
take abc.localhost for example, where %1 represents abc, and %2 would represent lcoalhost - With mod_rewrite enabled
<VirtualHost *:80> ServerAdmin root@localhost DocumentRoot "c:/projects/" ServerName localhost ServerAlias *.localhost RewriteEngine on RewriteCond %{HTTP_HOST} ^([^.]+)\.localhost$ [NC] RewriteRule ^/(.*)$ /%1/$1 </VirtualHost>
- With mod_rewrite and rewritemap
<VirtualHost *:80> ServerAdmin root@localhost DocumentRoot "c:/projects/" ServerName localhost ServerAlias *.localhost RewriteEngine on RewriteMap lowercase int:tolower # define the map file RewriteMap vhost txt:vhosts.map # deal with aliases as above RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$ # this does the file-based remap RewriteCond ${vhost:%1} ^(/.*)$ RewriteRule ^/(.*)$ %1/$1 </VirtualHost>
Then create the vhosts.map file to contain the hosts to documentroot map, e.g. abc.localhost c:/project_08/abc bcd.localhost c:/project_09/bcd
Jan
22
2009
22
2009
Setting up wildcard virtual hosts for web development environment
We used to have dev site hosted under sub directories of localhost, e.g. http://localhost/project/abc/. When it comes to development and testing, we really want to keep the environment as close as possible to the production environment, so it would lot easier to deploy and maintain. Imagine you have to refer to /images/logo.png from a page like /we/have/got/a/test/page/index.html, you really want absolute path in this senario, however in order to work with sub folder based dev environment you either having awful relative links (which would be easily break down when you relate files), or different absolute path as if you would deploy that on the production server. Which is not very practical at all.
To solve the problem, we thought about having separate virtual hosts for each project, ie. abc.localhost for abc project and so forth. There are several ways you could achieve this in apache configuration.