Apache 2 mod_rewrite and PHP. Change the value of $ _SERVER ['REQUEST_URI'] from htaccess?

I have this .htaccess file:

 RewriteEngine On RewriteRule ^hello$ goodbye RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !=/favicon.ico RewriteRule ^ index.php [L] 

So, I get all requests for index.php , but I get hello when I request hello , and I expected to get goodbye when printing $_SERVER['REQUEST_URI'] from PHP.

That is, $_SERVER['REQUEST_URI'] seems invulnerable even if the URL was rewritten before matching the RewriteRule with the index.php link. Is there any way to change this value?

I want to do this to add a thin and simple layer of URL preprocessing to existing code without modifying the PHP files. So I try to stick with .htaccess .

+8
php apache .htaccess mod-rewrite
source share
1 answer

First of all, you made a mistake by not adding the L or PT flag to your first rule. Your code should look like this:

 RewriteRule ^hello$ goodbye [PT] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !=/favicon.ico RewriteRule ^ index.php [L] 

Once this code has access to this variable in index.php :

 $_SERVER["REDIRECT_URL"] 

It will make a difference: /goodbye

EDIT

If mod_proxy enabled on your host, you can use your first rule as:

 RewriteRule ^hello$ /goodbye [P] 

And then you will have: $_SERVER["REQUEST_URI"]=/goodbye

+6
source share

All Articles