Apache mod_rewrite problem and% {REQUEST_URI}

Suppose we have the following PHP page "index.php":

<? if (!isset($_GET['req'])) $_GET['req'] = "null"; echo $_SERVER['REQUEST_URI'] . "<br>" . $_GET['req']; ?> 

and the following .htaccess file:

 RewriteRule ^2.php$ index.php?req=%{REQUEST_URI} RewriteRule ^1.php$ 2.php 

Now open access to "index.php". We get the following:

 /index.php null 

That's cool. Allow "2.php" access. We get the following:

 /2.php /2.php 

This is also cool. But now let's look at "1.php":

 /1.php /2.php 

So ... we ask for "1.php", it silently redirects to "2.php", which silently redirects to "index.php? Req =% {REQUEST_URI}", but here "% {REQUEST_URI}" seems to "2.php" (the page we are looking for after the first redirect), and $ _SERVER ['REQUEST_URI'] is "1.php" (the original request).

Should these variables be equal? Today I got a lot of headaches as I tried to make a redirect based only on the original request. Is there any variable that I can use in ".htaccess" that will tell me the original request even after the redirect?

Thanks in advance, and I hope I understand. This is my first post here :)

+4
source share
3 answers

Well, I think I solved the problem. I used the% {THE_REQUEST} variable, which basically contains something like this: "GET / 123.php HTTP / 1.1". It remains the same even after the redirect. Thank you all for your help! :)

+1
source

I'm not sure if it will satisfy your needs, but first try looking at REDIRECT_REQUEST_URI , and then, if it is not, REQUEST_URI . You mentioned in your comment on Gumbo that what you are really looking for is the original URI; REDIRECT_* Server variable versions are how Apache tries to make things available.

+4
source

Just reorder the rules and it works:

 RewriteRule ^1\.php$ 2.php RewriteRule ^2\.php$ index.php?req=%{REQUEST_URI} 

Or use only one rule:

 RewriteRule ^(1|2)\.php$ index.php?req=%{REQUEST_URI} 
+2
source

All Articles