Forum Moderators: phranque

Message Too Old, No Replies

Apache mod rewrite equivalent to IF logic

         

jusaf

10:29 am on Feb 9, 2011 (gmt 0)

10+ Year Member



I am now trying to achieve something like:


If( domain == 'www.domain1.com'){
writeCookie(domain1.com);
}
If( domain == 'www.domain2.com'){
writeCookie(domain2.com);
}


i tried the following, but i cant get it right:

RewriteEngine On
RewriteCond %{HTTP_X_FORWARDED_HOST} ^(www\.)?domain1\.com/ [NC]
RewriteRule .* - [CO=BALANCEID:balancer.lb2:.domain1.com] [L]
RewriteCond %{HTTP_X_FORWARDED_HOST} ^(www\.)?domain2\.com/ [NC]
RewriteRule .* - [CO=BALANCEID:balancer.lb2:.domain2.com] [L]
RewriteCond %{HTTP_X_FORWARDED_HOST} ^(www\.)?domain3\.com/ [NC]
RewriteRule .* - [CO=BALANCEID:balancer.lb2:.domain3.com] [L]


are those rules wrongly written or
that logic simply wont work?

PS: i also tried with HTTP_HOST instead of HTTP_X_FORWARDED_HOST but it had the same result.

g1smd

11:02 am on Feb 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The
HTTP_HOST
or
HTTP_X_FORWARDED_HOST
variable does not contain a trailing slash.

Do investigate if the latter variable is actually populated, by setting up a temporary test redirect to
example.com/%{HTTP_X_FORWARDED_HOST}
. Watch what happens when you request something from the server. You should be able to see exactly what is in that variable, as it will be shown in the path part of the URL displayed in the browser URL bar.

jusaf

4:03 pm on Feb 9, 2011 (gmt 0)

10+ Year Member



Thanks for the reply!

i did what u suggested (redirects for debugging) and HTTP_X_FORWARDED_HOST is not set, nor is HTTP_X_FORWARDED_FOR and HTTP_X_FORWARDED_SERVER.. strangely enough, from PHP HTTP_X_FORWARDED_HOST is set and with the correct domain that i need to retrieve.. as for HTTP_HOST, its set with the IP of the VirtualHost that is serving (this is in a load balanced scenario).

any idea how i can see all apache environment variables to see if there is one with the correct domain?
i thought they would be the same as the ones available in the PHP $_SERVER superglobal but apparently not..

jusaf

6:11 pm on Feb 9, 2011 (gmt 0)

10+ Year Member



So i've finally managed to make it work, the correct variable is HTTP:X-Forwarded-Host... thanks for the help anyway!