Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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 use this config in apache2 for wildcard subdomain and works good :

<VirtualHost *:80>
  DocumentRoot /var/www/
  ServerAlias *
  RewriteEngine On
  RewriteRule ^/(.*) /%{HTTP_HOST}/$1

this means, a request to will fetch the file /var/www/

but i want request to fetch file from /var/www/user/foo.html.

which wildcard should am i use for this?

share|improve this question
up vote 3 down vote accepted

Set rewrite rule grab the sub.domain and redirect to /sub/requestedFile

RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9-]+)\$
RewriteRule ^(.+) /%1/$1 [L,QSA]

Or set VirtualDocumentRoot with %-1 representing the sub domain.

<VirtualHost *:80>
  VirtualDocumentRoot /var/www/%-1
  ServerAlias *
share|improve this answer
works like a charm! thanks :) – Amir Molaa Apr 13 '13 at 12:28

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.