- CSS - Cascaded Style Sheets



|
|
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.
DocumentRoot /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 /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.
www.yourdomain.com => /webhosts/www/
muon.yourdomain.com => /webhosts/muon/
Finally setup your domain as wildcard domain *.yourdomain.com in your DNS and off the fun goes!
Last-Modified: Sat, 04 Feb 2006 16:03:19 GMT
|
|