- 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.
4 Comments
RSS feed for comments on this post.
great post..thanks..
i’ve try add this code to httpd.conf :
ServerAdmin root@localhost
DocumentRoot “d:/phpfiles/”
ServerName localhost
ServerAlias *.localhost
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+)\.localhost$ [NC]
RewriteRule ^/(.*)$ /%1/$1
after restart apache, i try access tes.localhost, but Firefox always treat it like a ordinary website ,with adding “www” => http://www.tes.localhost , so Firefox return “Address Not Found”. are there other program needed like dns ,etc ? thx
Hi Darwin, you may need to add entries to your hosts file or if you have access to a DNS server, make a wildcard subdomain of localhost to point to 127.0.0.1
Hi,
I’d like to provide to users create virtual subdomain. For example like a google, who write anything to the search bar and click create, it would be created. click create, http://letsgohome.asdasdasd.com