I've got a wildcard DNS entry for *.sites.example.com
I have a virtual host setup for sites.example.com and a set of rewrite rules to direct the browser to a directory of the same name as the variable bit of the domain name. For example:
johndoe.sites.example.com -> would redirect to: clients/johndoe behind the scenes. the clients directory basically containing any existing subdomain possibility.
Here's what I've hobbled together so far:
- Code: Select all
VirtualHost *:80>
ServerName sites.example.com:80
ServerAdmin webmaster@example.com
DocumentRoot /data/sites/sites.example.com/htdocs
ErrorLog logs/sites.example.com-error_log
CustomLog logs/sites.example.com-access_log common
Alias /cms/ "/common_code_for_all_sites/cms/"
Alias /index.php "/common_code_for_all_sites/index.php"
<Directory /data/sites/sites.example.com/htdocs/>
Options +FollowSymlinks -Indexes
RewriteEngine on
RewriteBase /
# RULE 1 - add slash to the URL subdir if there is none
RewriteCond %{HTTP_HOST} !^(www\.)?sites\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.sites\.example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^clients/([^/]+)/(.*)$ http://%{HTTP_HOST}/$2index.php [L,R]
# RULE 2 - rewrites http://something.sites.example.com/ to http://sites.example.com/clients/something/
RewriteCond %{HTTP_HOST} !^(www\.)?sites\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.sites\.example\.com$ [NC]
RewriteRule !^clients/(.*)$ clients/%1%{REQUEST_URI} [QSA,E=DIR:%1,L]
</Directory>
</VirtualHost>
Now, the problem I'm facing is that when the aliased code is accessed, it needs to know what the unique variable is. For johndoe.sites.example.com - it would need the info 'johndoe'. However I can't rely on some regexp parsing of the domain name from within the code, because it's possible that the user could also be accessing the same site via a 'real' domain name. ie. johndoe.com
So my solution was to set an ENV variable, like you see in RULE 2, E=DIR:%1 - From what I understand this should be setting the ENV variable DIR=johndoe - But this ENV variable is never set and I have no idea why, because the rest of the rule seems to be working.
Any help, corrections, advice, etc. would be greatly appreciated. I'm banging my head against a wall
Thanks,
Nick