nginxurl-rewriting

nginx rewrite WITHOUT change url


I want to use rewrite function in my nginx server.

When I try "http://www.example.com/1234", I want to rewrite "http://www.example.com/v.php?id=1234" and want to get "http://www.example.com/1234" in browser.

Here is nginx.conf file

...
  location ~ /[0-9]+ {
      rewrite "/([0-9]+)" http://www.example.com/v.php?id=$1 break;
  }
...

When I try "http://www.example.com/1234"

I want to ...

url bar in browser : http://www.example.com/1234
real url : http://www.example.com/v.php?id=1234

but I'm in trouble ...

url bar in browser : http://www.example.com/v.php?id=1234
real url : http://www.example.com/v.php?id=1234

Solution

  • Reference: https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite

    If the replacement string begins with http:// then the client will be redirected, and any further >rewrite directives are terminated.

    So remove the http:// part and it should work:

    location ~ /[0-9]+ {
            rewrite "/([0-9]+)" /v.php?id=$1 break;
    }