subdomain rewrite & setting of environment variable

Using a single web hosting account to host multiple sites

subdomain rewrite & setting of environment variable

Postby nkj » Fri May 15, 2009 12:05 pm

Hello All,

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 :o

Thanks,
Nick
nkj
 
Posts: 21
Joined: Fri May 15, 2009 11:32 am

Postby richardk » Sat May 16, 2009 9:08 am

Is REDIRECT_DIR set?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby nkj » Sat May 16, 2009 10:26 am

richardk wrote:Is REDIRECT_DIR set?


Hi Richard, thanks for the response. I'm not familiar with REDIRECT_DIR, I searched the apache docs and was unable to come up with anything. What's it for?

Thanks,
Nick
nkj
 
Posts: 21
Joined: Fri May 15, 2009 11:32 am

Postby nkj » Sun May 17, 2009 5:28 am

richardk wrote:Is REDIRECT_DIR set?


Any other suggestions? I'm a bit confused as to what REDIRECT_DIR is and how it's supposed to be used. Is it related to setting environment variables from within the apacheconf?
nkj
 
Posts: 21
Joined: Fri May 15, 2009 11:32 am

Postby richardk » Sun May 17, 2009 9:02 am

It's a variable just like DIR would be. The prefix REDIRECT_ has most likely been added.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby nkj » Sun May 17, 2009 3:43 pm

richardk wrote:It's a variable just like DIR would be. The prefix REDIRECT_ has most likely been added.


Can you give me an example of what you are suggesting? I still am confused as to what you mean.
nkj
 
Posts: 21
Joined: Fri May 15, 2009 11:32 am

Postby richardk » Mon May 18, 2009 11:10 am

Code: Select all
$_SERVER['REDIRECT_DIR']

instead of
Code: Select all
$_SERVER['DIR']
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby nkj » Mon May 18, 2009 3:15 pm

richardk wrote:
Code: Select all
$_SERVER['REDIRECT_DIR']

instead of
Code: Select all
$_SERVER['DIR']


I guess thats what confuses me because i dont use $_SERVER['DIR'] anywhere, i don't know what you mean.

sorry if I'm being dense. like i said originally, i've been beating my head against the wall and maybe i'm missing something obvious.. but don't know that till it's, obvious :)

thanks again
-nick
nkj
 
Posts: 21
Joined: Fri May 15, 2009 11:32 am

Postby richardk » Tue May 19, 2009 3:40 pm

When you set the variable a prefix is (probably) added. Imagine
Code: Select all
E=DIR:%1

is
Code: Select all
E=REDIRECT_DIR:%1

instead.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby nkj » Wed May 20, 2009 1:28 pm

richardk wrote:When you set the variable a prefix is (probably) added. Imagine
Code: Select all
E=DIR:%1

is
Code: Select all
E=REDIRECT_DIR:%1

instead.


Ah, I see what you mean (for some reason I thought you mean there was a REDIRECT_DIR directive in the apache conf, which was confusing me).

I checked it and no luck. If I set it to TEST:test and dump all the environment variables, the words TEST or test never show up:

Code: Select all
RewriteCond %{HTTP_HOST} !^(www\.)?sites\.example\.com$    [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.sites\.example\.com$ [NC]
RewriteRule !^clients/(.*)$ clients/%1%{REQUEST_URI} [QSA,E=TEST:test,L]


Still nada.

Is all my syntax correct here? I have no idea whats going on, all the documentation I have read it seems pretty straight forward. If the redirect is even working, then the environment variable should be set - the redirect DOES work, but the environment variable is never set.
Last edited by nkj on Thu May 21, 2009 3:42 am, edited 2 times in total.
nkj
 
Posts: 21
Joined: Fri May 15, 2009 11:32 am

Next

Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 1 guest

cron