phpurlurl-rewriting

Apache rewrite - get original URL in PHP


I have a rewrite in NGINX or Apache for this address:

http://example.com/hello

to a script like

http://example.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? The following is the content of my NGINX configuration file:

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 example.com;

    
    location /
    {
    rewrite ^/(main|best|air)$ /core/feeds.php?act=$1 last;
    proxy_pass http://127.0.0.1:8080;
    }
}

Solution

  • In the NGINX configuration, you 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'];
    

    Update

    For some reason NGINX doesn't like the _ symbol in headers name. I 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']