Page 1 of 1
Sending parameters to rewriterule
Posted: Sat Jun 21, 2003 10:02 pm
by acerola
I need to make a php script that would send a server variable or environment variable or cookie or something that carries information to an Apache RewriteRule.
Something like this:
<?php
$_ENV['NEWDIR'] = 'fullpath';
?>
And then on http.conf:
RewriteRule ^/(.*)olddir(.*) /$1%{ENV:NEWDIR}$2
So, if you try to acess /olddir/image.jpg before running the script, you would get nowhere. But if you run the script first, you would get to /fullpath/image.jpg.
The problem is that apache can't see the variable php just set.
Posted: Sat Jun 21, 2003 11:08 pm
by volka
the rewrite rules apply to any request regardless of the user/client.
So one user requesting the right script would enable the link for all others.
Do you want that?
Posted: Sat Jun 21, 2003 11:38 pm
by acerola
No, I don't want that. But I saw on the rewrite_mod documentation that you could read server variables and environment variables, including http_cookies. I thought it would be possible to read cookies, which are client/user specific. And I think environment variables are also client/user specific, are they not?
Posted: Sat Jun 21, 2003 11:40 pm
by acerola
I created a test rewriterule like this:
RewriteRule ^/(.*)olddir(.*) /$1%{http_REFERER}$2
It changed every time, as the referer changed. This rule is not the same for every user/client.
Posted: Sun Jun 22, 2003 1:10 am
by volka
ah, ic

cookies should be available in the server variable HTTP_COOKIE
The format will be
urlencoded.
Posted: Sun Jun 22, 2003 1:43 am
by acerola
I tryed setting the cookie with php, but I can't get rewriterule to see it. When I tall rewriterule to output it, it prints "".
Posted: Sun Jun 22, 2003 3:24 am
by volka
this small example works fine for me
Code: Select all
RewriteEngine on
RewriteCond %{HTTP_COOKIE} cookieSet=true
RewriteRule ^/test.php /test.php їl,E=cookieSet:true]
Code: Select all
<?php // /test.php
if (isset($_SERVER['cookieSet']))
echo 'cookie set and recognized by mod_rewrite';
else
{
setcookie ('cookieSet', 'true', time()+300);
?><html><body>
<pre><?php print_r($_SERVER); ?></pre>
<a href="test.php">reload</a>
</body></html>
<?php
}
?>
Posted: Sun Jun 22, 2003 1:25 pm
by acerola
Works great. Thanks! I'm gonna use something like this:
Code: Select all
RewriteEngine on
RewriteCond %{HTTP_COOKIE} ^.*targetcookie=((їa-z]|їA-Z]|ї0-9])*).*$
RewriteRule ^/test\.php$ /test.php?target=%1 їL]
Now I can put the contents of a cookie anywhere in the rewriterule.
The problem was that I wasn't using the setcookie function in php. I was changing the value of http_cookie manually. Also, it seems that environment variables for apache are server variables for php. Obvious? Yeah, but I didn't get it before.
