The Apache Haus Forum

Advanced search  

News:

Welcome to Apache Haus Distribution Forum

Pages: [1]   Go Down

Author Topic: Dynamic DocumentRoot for Sub-Domains with RewriteEngine  (Read 4894 times)

mario

  • Administrator
  • Member Elite
  • *****
  • Offline Offline
  • Posts: 580
Dynamic DocumentRoot for Sub-Domains with RewriteEngine
« on: January 06, 2009, 05:38:13 PM »

Dynamic DocumentRoot for Sub-Domains with RewriteEngine/RewriteCond/RewriteRule

If you have plenty of sub-domains and every subdomain routes in another directory on the server, it's annoying to add every subdomain to the apache.conf (httpd.conf) file.

This can be done dynamically.

Code: [Select]
DocumentRoot c:/webhosts/

[...]

<VirtualHost *>
ServerAlias www.yourdomain.com
ServerName www.yourdomain.com
RewriteEngine  on
RewriteCond    %{HTTP_HOST}  ^yourdomain.com
RewriteRule    ^(.*)$        /www/$1 [L]
RewriteCond    %{HTTP_HOST}  ^www.*
RewriteRule    ^(.*)$        /www/$1 [L]
RewriteCond    %{HTTP_HOST}  ^(.*)\.yourdomain\.com
RewriteRule    ^(.*)$        /%1/$1 [L]
</VirtualHost>

As you can see, the DocumentRoot is c:/webhosts/. This is the directory where all other sub-directories for the sub-domains are located.

We use the RewriteEngine to decide which directory we want. The first RewriteCond is to route the domain without any sub-domain into the www directory. The second RewriteCond demonstrates how the third RewriteCond works with any subdomain.

Code: [Select]
www.yourdomain.com  =>  c:/webhosts/www/
muon.yourdomain.com  =>  c:/webhosts/muon/
Logged
Pages: [1]   Go Up
 

Sitemap 1 2 3 4 5 6 7 8 9 10 11 12 13