nginx重写而无需更改URL [英] nginx rewrite WITHOUT change url
问题描述
我想在我的Nginx服务器中使用重写功能.
I want to use rewrite function in my nginx server.
当我尝试" http://www.example.com/1234 "时,我想重写" http://www.example.com/v.php?id=1234 并希望获得" http://www.example.com/1234 ".
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.
这是nginx.conf文件
Here is nginx.conf file
...
location ~ /[0-9]+ {
rewrite "/([0-9]+)" http://www.example.com/v.php?id=$1 break;
}
...
当我尝试" http://www.example.com/1234 "
我想...
url bar in browser : http://www.example.com/1234
real url : http://www.example.com/v.php?id=1234
但我有麻烦...
url bar in browser : http://www.example.com/v.php?id=1234
real url : http://www.example.com/v.php?id=1234
推荐答案
参考: http://wiki. nginx.org/HttpRewriteModule#rewrite
如果替换字符串以http://开头,则客户端将被重定向,并且任何其他> rewrite指令都将终止.
If the replacement string begins with http:// then the client will be redirected, and any further >rewrite directives are terminated.
因此,删除http://部分,它应该可以工作:
So remove the http:// part and it should work:
location ~ /[0-9]+ {
rewrite "/([0-9]+)" /v.php?id=$1 break;
}
这篇关于nginx重写而无需更改URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!