Sending parameters to rewriterule
Moderator: General Moderators
Sending parameters to rewriterule
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.
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.
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?
ah, ic 
cookies should be available in the server variable HTTP_COOKIE
The format will beurlencoded.
cookies should be available in the server variable HTTP_COOKIE
The format will be
Code: Select all
cookie1=value1; cookie2=value2this 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
}
?>Works great. Thanks! I'm gonna use something like this:
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.
Code: Select all
RewriteEngine on
RewriteCond %{HTTP_COOKIE} ^.*targetcookie=((їa-z]|їA-Z]|ї0-9])*).*$
RewriteRule ^/test\.php$ /test.php?target=%1 їL]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.