Jan
22
2009
4

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.
  1. 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
  2. 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>
  3. 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
Both method 1 and 2 would wildcard map *.localhost to c:/projects/*, and method 3 will allow you to specify documentroot of each domain in the vhosts.map file. With extra virtualhost you don't need to alter httpd.conf any more, all you need to do is create the subdomain and create the project folder in appropirate location. Since the hosts file doesn't support wildcard entry, for each of the subdomain of localhost you may need to create a separate entry, however it should only take few seconds, and nothing needs to be restart to take effect, which is easy and good enough. Alternatively if you have access to your DNS server, you may setup wildcard on the localhost zone, so everyone in the dev team will have access to *.localhost without alter anything in the local hosts file. Hope whis will in a way help web developers to deliver project faster.
Written by in: Web Development | Tags:

Copyright Orite Group Pty Ltd | Powered by WordPress | Theme: Aeros 2.0