Forum Moderators: phranque

Message Too Old, No Replies

mod-rewrite url to hide from url yet pass variable

         

BPeru

12:06 pm on Sep 13, 2006 (gmt 0)

10+ Year Member



From what I have read about mod-rewrite I understand that some kind of environment variable can be set to pass a variable. I have spent hours researching this and still don't understand how it's done.

For example www.widget.com/widget.php?color=green or www.widget.com/widget.php?color=red are to be mod-rewriten to www.widget.com/widget.php yet the value of the variable "color" is still passed in the background and can be "output" on the widget.php page example: Looking for green widgets?

Can anyone point me in the right direction.

matc

1:03 am on Sep 14, 2006 (gmt 0)

10+ Year Member



Hi,

I get the impression you don't need to look into environment variables, however, I do take it from what you've said that you want to wrap a redirect up in a .htaccess file using mod_rewrite...

The way I'm thinking about this at the moment is it depends on the version of apache you are running to how you could do this, theres a number of subtle differences as always, what version are you running?

Also you know within PHP you can pick the value of any key:value pairs on the query string by calling its key name... so what version of PHP are you running? Anyway as always with a big warning to check any params before directly out putting it into your page... ;)

Anyway, try looking at 'qsappendĤQSA' in the Apache docs under RewriteRule if you want to push on more data to an existing query string, or try looking at %{QUERY_STRING} and just append that you you final redirect when all conditions 'RewriteCond' have been met. However, if you wanted to remove the previous query string to nothing then just put a? at the end of the final redirected uri.

Anyway, let us know what you're thinking,

cheers, matc

BPeru

5:24 am on Sep 14, 2006 (gmt 0)

10+ Year Member



Thanks for the feedback Matc,

I'm running PHP4. Yes, in the meantime I have found it's not an environment variable that gets set maybe it's a global variable?

Basically I'm trying to redirect

widgets.php?key=green
to
widgets.php

most likely with mod rewrite, but before redirecting setting a variable (in the background) to "green"(or whatever key happens to be) which get passed and can be accessed via php and output to the widgets.php page.

matc

2:14 pm on Sep 14, 2006 (gmt 0)

10+ Year Member



Hi'ya,

I believe that to set a environment variable and redirect at the same time within Apache you could try something like this... I'm more use to apache2 thou..

Untested of course :)

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} ^widgets.php$
RewriteCond %{QUERY_STRING} colour=([a-z]+)
RewriteRule ^(.*)$ widgets.php? [E=set.env.var:%1,R,L]

However, if you just wanted to set an environment variable without the redirect you could do something like...

RewriteRule ^(.*)$ - [E=set.env.var:%1,L]

Cheers, matc

matc

3:00 pm on Sep 14, 2006 (gmt 0)

10+ Year Member



Hi,

Forgot but heres something worth a look for accessing Env variables from within PHP... you could use this..
[uk.php.net...]
cheers, matc

jdMorgan

8:44 pm on Sep 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Be aware that HTTP is a stateless protocol. Therefore, any variable set before the redirect will not be available to Apache while processing the subsequent client HTTP request that results from the redirect. In other words, Apache won't know that the client has made any previous requests, because each and every HTTP request is handled separately, with no server knowledge of previous requests.

The only ways to pass 'session' information from an initial client request to a subsequent client request is by using cookies. And of course, this won't work if the client has disabled cookies...

I would re-examine your need to do this redirect, and decide if it is worth the complications it causes. You might be happier implementing the more common 'search engine friendly' static URLS like many other sites. This subject is well-covered here, and a search on WebmasterWorld will turn up dozens of threads on the subject.

Jim

BPeru

7:08 am on Sep 15, 2006 (gmt 0)

10+ Year Member



Thanks Matc and Jim,

Jim, Is it that a variable can be assigned and passed like discussed here, but the concern that two requests very close to each other may mix up the variables? if so that is unlikely as this isn't a high volume site.

No it's not for search engine friendly urls ;)

jdMorgan

1:49 pm on Sep 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, you cannot pass a variable from one HTTP request to another on the server side using only Apache.

A redirect terminates the current HTTP request and tells the client (browser, robot, etc) to make a new request.

Apache has no memory of previous requests, and has no expectations of subsequent requests.

It handles one request, finishes that, then handles another request. Within Apache, the second request is handled as if the first request never happened.

So Apache cannot remember anything that happened in the first request while handling the second request.

Solutions require action outside of Apache, either by the client, or by a server-side script.

The three general solutions are:

  • Set a client-side cookie, which it will then send back to the site with every subsequent request
  • Build a database on the server where you log the state of each client based on his login ID
  • Put the client state into the redirect URL as a query string. This solution defeats your purpose, though, because you want 'clean' URLs without query string parameters attached.

    Those are the only ways to pass information from one request to the next.

    Jim

  • BPeru

    9:48 pm on Sep 19, 2006 (gmt 0)

    10+ Year Member



    Thank you Jim, sorry was away for abit..

    I admire your insight, thorough posts. Definitely top contender for Webmaster World's MVM!

    So looks like cookies is the easiest way to go.

    I will have to read up on Apache::Session and apache in general

    lmo4103

    2:14 pm on Sep 23, 2006 (gmt 0)

    10+ Year Member



    Another discussion at How Do I Rewrite PHPSESSID's? [webmasterworld.com]