Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to setup zero-config web-development environment: wildcard DNS plus dynamic virtual hosts. Here is my v-hosts config:

NameVirtualHost *:80

<VirtualHost *:80>
ServerAlias *.dev               
    VirtualDocumentRoot /var/sites/%-2/docs
    UseCanonicalName Off

At this point everything is ok, but now I'm trying to setup dymanic subdomains with fallback:           → /var/sites/test/docs       → /var/sites/test/sub → /var/sites/test/docs

Directory structure:


What i currently try:

<Directory "/var/sites">
    RewriteEngine On
    RewriteCond %{ENV:REDIRECT_SUBDOMAIN} ="" 
    RewriteCond %{HTTP_HOST} ^([a-z0-9][-a-z0-9]+)\.([a-z0-9][-a-z0-9]+)\.dev\.?(:80)?$ [NC] 
    RewriteCond %{DOCUMENT_ROOT}/%2/%1 -d 
    RewriteRule ^(.*) /%2/%1/$1 [E=SUBDOMAIN:%1,L]

And now stacked at:

[perdir /var/sites/] strip per-dir prefix: /var/sites/test/docs/ -> test/docs/
[perdir /var/sites/] applying pattern '^(.*)' to uri 'test/docs/'
[perdir /var/sites/] RewriteCond: input='' pattern='=' => matched
[perdir /var/sites/] RewriteCond: input='' pattern='^([a-z0-9][-a-z0-9]+)\.([a-z0-9][-a-z0-9]+)\.dev\.?(:80)?$' [NC] => matched
[perdir /var/sites/] RewriteCond: input='/var/sites/test/sub' pattern='-d' => matched
[perdir /var/sites/] rewrite 'test/docs/' -> '/test/sub/test/docs/'
setting env variable 'SUBDOMAIN' to 'sub'
[perdir /var/sites/] internal redirect with /test/sub/test/docs/ [INTERNAL REDIRECT]
[perdir /var/sites/] add path info postfix: /var/sites/test/docs/test -> /var/sites/test/docs/test/sub/test/docs/
[perdir /var/sites/] strip per-dir prefix: /var/sites/test/docs/test/sub/test/docs/ -> test/docs/test/sub/test/docs/
[perdir /var/sites/] applying pattern '^(.*)' to uri 'test/docs/test/sub/test/docs/'
[perdir /var/sites/] RewriteCond: input='sub' pattern='=' => not-matched
[perdir /var/sites/] pass through /var/sites/test/docs/test

Any ideas?

Is that even possible?


share|improve this question

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.