Skip to main content
 首页 » 编程设计

nginx 重写而不更改 url

2024年12月31日12lexus

我想在我的 nginx 服务器中使用重写功能。

当我尝试“http://www.example.com/1234 ”时,我想重写“http://www.example.com/v.php?id=1234 ”并希望在浏览器中获得“http://www.example.com/1234 ”。

这是 nginx.conf 文件

... 
  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

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



所以删除 http://部分,它应该可以工作:
location ~ /[0-9]+ { 
        rewrite "/([0-9]+)" /v.php?id=$1 break; 
}