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
The syntax of rewrite directive is: rewrite regex replacement-url [flag]; regex: The PCRE based regular expression that will be used to match against incoming request URI. replacement-url: If the regular expression matches against the requested URI then the replacement string is used to change the requested URI.
Permanent redirects such as NGINX 301 Redirect simply makes the browser forget the old address entirely and prevents it from attempting to access that address anymore. These redirects are very useful if your content has been permanently moved to a new location, like when you change domain names or servers.
It is exactly the $uri/ part that makes nginx assuming an URI can be a directory name and looking for an index file presence inside it.
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; }
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