I have a rewrite in nginx or Apache for this address:
http://domain.com/hello
to a script like
http://domain.com/test.php&ref=hell
How can I access this rewritten URL in PHP? Because, if I use $_SERVER['REQUEST_URI']
of course I get:
/test.php&ref=hell
but I only want:
/hello
Is this possible? Thanx for help.
Upd nginx cnf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
server
{
listen 80;
server_name domain.test;
location /
{
rewrite ^/(main|best|air)$ /core/feeds.php?act=$1 last;
proxy_pass http://127.0.0.1:8080;
}
}
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.
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.
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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With