logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> How-to's & Documentation & Tips View previous topic :: View next topic
Reply to topic   Topic: Dynamic DocumentRoot for Sub-Domains with RewriteEngine
Author
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7288
Location: Germany, Next to Hamburg

PostPosted: Tue 08 Jul '08 12:13    Post subject: Dynamic DocumentRoot for Sub-Domains with RewriteEngine Reply with quote

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:

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:

www.yourdomain.com  =>  c:/webhosts/www/
muon.yourdomain.com  =>  c:/webhosts/muon/
Back to top


Reply to topic   Topic: Dynamic DocumentRoot for Sub-Domains with RewriteEngine View previous topic :: View next topic
Post new topic   Forum Index -> How-to's & Documentation & Tips