Forum Moderators: phranque
I'm fairly new to all this and inevitably struggling a bit with mod_rewrite. The problem in this case is to do with environmental variables.
I want to redirect certain visitors on the basis of the referring site they used to arrive at mine - but the redirect would only apply in one directory comprising a small portion of the site. I am assuming they will arrive at pages outside this directory, therefore the referer will no longer be that of the external site.
I had assumed I could detect their referral from the other site and set an enviroment variable like:
RewriteCond %{HTTP_REFERER} ^h*tp://www.thatdomain.com [NC]
RewriteRule ^.* - [E=that_site:yes]
So that when they arrived at the pages I wished them to see alternative versions of, I could use the variable to determine how they got there. The variable is being set, but disappears when the next link is clicked on, the logic of which escapes me. I've searched this forum, the apache docs etc, etc, but can find nothing unambiguous about about the duration of variables. I know I could use cookies for this, but its a small thing needed and I dont really want to have to learn One More Thing just to do it.
I have a couple of questions here;
1) Is there a way to make the variable "stick" over multiple pages?
2) What is the point of setting environmentals if they are NOT persistent? After all, if you were doing the detection on the basis of REMOTE_HOST or REMOTE_ADDR, there would be no point setting a variable since you could detect host or IP every time.
The version of Apache is 1.3.26 and the lines are being used in an .htaccess file.
My forehead is bleeding from the headbanging! Help appreciated.
Welcome to WebmasterWorld [webmasterworld.com]!
This is a common problem with understanding servers. To make it simple, just remember that each HTTP request such as a click, a redirect, or a request for an included image, script, or stylesheet stands alone - the server does not associate any of them with the loading of the initial page. A server sees a GET, locates the requested resource, serves it, and closes the connection. Therefore, "persistence" is meaningless in the server context; Variables persist only from the time a resource is requested until such time that it has been served.
In order to preserve variables across a "session", you'll need to include the variable in the URL or query string (e.g. a session ID), store it on the user's computer as a cookie, or redirect to a unique subdomain based on the referer.
However, there's another -- and bigger -- problem, and that is that referrers are unreliable; They can be easily faked, and they are often blocked or dropped by intervening caches, proxies, and firewalls between the user and your server. And much of the time, the user is completely unaware that the referrer is being blocked, and has no control over it. Also, pages and images loaded by JavaScript links, typed-in directly to the browser address bar, or loaded using a right-click context menu such as "Open in new window" will have no logical referrer. A good solution must comprehend all of these possibilities.
Jim
I suppose I could append the results of the variable to the URL, although I am a little reluctant as I imagine that means slowing the server with additional mod_rewrite instructions.
From what I've read it is easy enough to set a cookie (is it possible via htaccess? I dont have access to httpd.conf); how easy is it for mod_rewrite to access the cookie contents?
One stupid question: why would you set an environmental variable then if it is NOT persistent?
Many thanks for your comprehensive reply to my previous.
Mark
I make extensive use of environment variables to save off information about referers, user-agents, and browser capabilities. However, the scope of these variables is limited to the single HTTP request being processed. I have very limited need for any session-level variables, and will therefore defer to anyone who might use them more actively.
Jim