Forum Moderators: phranque

Message Too Old, No Replies

Variable Environment Variables?

         

bakedjake

8:48 pm on Mar 11, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



So here's a weird one for you guys:

I have a program that needs an environment variable (normally set in .htaccess or httpd.conf) to render content.

If I sent CONTENT=A then it renders one set of content, CONTENT=B renders another set of content. Right now I've simply got the variable set using SetEnv set to CONTENT=A.

I'm looking for an elegant solution to switch between content sets based on:

1. Location (aka mod_geoip)
2. User initiated action (user clicking on link - like /contenta or /contentb which would just fire rewrites/redirects)

Obviously serving content based on location would be the normal case but once the user initiated action happens I would not want to override that action unless the user switches back.

I'm looking at doing this in apache for portability and for ease of upgrade.

Essentially, these are the steps I'm thinking of implementing:

1. Check for presence of cookie with CONTENTA or CONTENTB variable set using mod_rewrite HTTP_COOKIE conditional. If that cookie is set, insert appropriate environment variable using mod_rewrite. (I'm pretty sure I can set environment variables as part of a substitution; does anyone know for sure?)

2. If no cookie is set, check location via mod_geoip, set cookie through mod_headers, and loop back through #1. (do this so mod_geoip only fires on session initiation)

3. If user clicks requests "switcher" link /contenta or /contentb, set the appropriate cookie through mod_headers, set the environment variables, and redirect the user back to the page where they came from (can you use REFERRER variables in substitution rules like that?)

Does that make sense? Am I missing something? I want to do all of this in mod_rewrite so I can use conditionals

jdMorgan

10:16 pm on Mar 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> (I'm pretty sure I can set environment variables as part of a substitution; does anyone know for sure?)
Yes, apache.org knows for sure. See RewriteRule [E=var:val] documentation...

Note: RewriteRule can set cookies (on Apache 2.x and later only), RewriteCond can be used test them (on any Apache version).

Do not use referrers. They can be spoofed. Also, any direct type-in, JavaScript link, or media player link requests, and potentially all requests coming through any caching proxy server (e.g. all AOL and EarthLink users) will have no referrer. Use a cookie instead (possibly sent with the redirect itself -- I've never tested this myself).

Make sure your implementation fails gracefully if the visitor has disabled cookies, or test for cookie support before even starting the geo-location-redirection process.

Jim