1

i use this config in apache2 for wildcard subdomain and works good :

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

this means, a request to http://user.example.com/foo.html will fetch the file /var/www/user.example.com/foo.html.

but i want request to http://user.example.com/foo.html fetch file from /var/www/user/foo.html.

which wildcard should am i use for this?

Amir Molaa
  • 115
  • 6

1 Answers1

3

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

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

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

<VirtualHost *:80>
  VirtualDocumentRoot /var/www/%-1
  ServerName sumdomains.example.com
  ServerAlias *.example.com
</VirtualHost>
David Houde
  • 3,200
  • 1
  • 16
  • 19