Apache rewrite - get original URL in PHP
Solution 1:
It really depends on the PHP setup. With mod_php you oftentimes still have the original request path in REQUEST_URI
. For CGI or FastCGI setups it is quite commonly REDIRECT_URL
. You will have to check a phpinfo()
page to be sure.
If you really can't find anything that would help, then it's time for cheating! You can adapt your RewriteRule like this to retain the original URL in an environment variable of your chosing:
RewriteRule ^(\w+)$ test.php?ref=$1 [E=ORIG_URI:/$1]
This would then be available as $_SERVER["ORIG_URI"]
, or you can just get it from the URI with $_GET['ref'].
But you would have to use this trick on all potential RewriteRules.
Solution 2:
You can usually find the requested URL in
$_SERVER['REQUEST_URI']
-
$_SERVER['REDIRECT_URL']
(maybe Apache only, don't know about nginx)
I know you mentioned $_SERVER['REQUEST_URI']
contains your rewritten URL but in all my tests, it contains the original request.
Why don't you dump $_SERVER
and see what's in there.
Solution 3:
In Nginx
conf, we need to add user header with request_uri
:
proxy_set_header request_uri $request_uri;
And read it in php
:
echo $_SERVER['HTTP_REQUEST_URI'];
upd
for some reason nginx don't like symbol '_' in header name, don't know how it worked before, maybe something changed after nginx update. Now i'm using
proxy_set_header rewriteduri $request_uri;
and in php
$_SERVER['HTTP_REWRITEDURI']