nginx rewrite WITHOUT change url

user1850593 picture user1850593 · Mar 10, 2013 · Viewed 46.6k times · Source

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

Answer

Chuan Ma picture Chuan Ma · Mar 10, 2013

Reference: http://wiki.nginx.org/HttpRewriteModule#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;
}