Apache 2 mod_rewrite and PHP. Modify $_SERVER['REQUEST_URI'] value from htaccess?
Asked Answered
R

1

10

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'm receiving all the requests on index.php, but I get hello when asking for hello, and I expected to receive goodbye when printing $_SERVER['REQUEST_URI'] from PHP.

That is, $_SERVER['REQUEST_URI'] seems inmutable, even when the URL has been rewritten already before matching the RewriteRule referring index.php. Is there any way to modify this value?

I want to do this to add a thin and simple layer of URL preprocessing to some existing code, without modifying the PHP files. So I'm trying to stick inside the .htaccess.

Rehearse answered 4/9, 2013 at 15:38 Comment(2)
#5493575Myer
Yes, the common approach is to set a new environment variable with [E=]Bergin
D
8

First of all you made a mistake of not putting L or PT flag in your first rule. Your code should be like this:

RewriteRule ^hello$ goodbye [PT]

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

Once that code is there access this variable in index.php:

$_SERVER["REDIRECT_URL"]

This will have value: /goodbye

EDIT

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

RewriteRule ^hello$ /goodbye [P]

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

Disengagement answered 4/9, 2013 at 15:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.