Sending parameters to rewriterule

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
acerola
Forum Newbie
Posts: 6
Joined: Sat Jun 21, 2003 9:55 pm

Sending parameters to rewriterule

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
acerola
Forum Newbie
Posts: 6
Joined: Sat Jun 21, 2003 9:55 pm

Post 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?
acerola
Forum Newbie
Posts: 6
Joined: Sat Jun 21, 2003 9:55 pm

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

ah, ic ;)
cookies should be available in the server variable HTTP_COOKIE
The format will be

Code: Select all

cookie1=value1; cookie2=value2
urlencoded.
acerola
Forum Newbie
Posts: 6
Joined: Sat Jun 21, 2003 9:55 pm

Post 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 "".
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

this small example works fine for me

Code: Select all

RewriteEngine  on
RewriteCond %&#123;HTTP_COOKIE&#125; cookieSet=true
RewriteRule ^/test.php /test.php &#1111;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
}
?>
acerola
Forum Newbie
Posts: 6
Joined: Sat Jun 21, 2003 9:55 pm

Post by acerola »

Works great. Thanks! I'm gonna use something like this:

Code: Select all

RewriteEngine on
RewriteCond %&#123;HTTP_COOKIE&#125; ^.*targetcookie=((&#1111;a-z]|&#1111;A-Z]|&#1111;0-9])*).*$
RewriteRule ^/test\.php$ /test.php?target=%1 &#1111;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. :wink:
Post Reply