nginx 重写而不更改 url

标签 nginx rewrite

我想在我的 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;
}

关于nginx 重写而不更改 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15322826/

相关文章:

nginx - 如何修复 'failed (22: Invalid argument) while sending response to client'

python - 根目录下静态站点的 Nginx 配置,子目录下的 Flask 应用

web.config 中的重定向规则

string - 仅当 URI 不包含特定字符串时才进行 301 重定向

post - 在 nginx 中是否可以将 POST 请求重写为 GET 请求?

regex - 在 Nginx 重写中处理多个参数

deployment - Nginx 位置、别名、重写、root

python - 如何找出 uWSGI 杀死 worker 的原因?

amazon-web-services - aws ubuntu 16.04 服务器上的 HTTPS

codeigniter - 子目录中 Joomla 和 Codeigniter 的 Nginx 配置