【问题标题】:NGINX rewrite regex not working correctlyNGINX 重写正则表达式无法正常工作
【发布时间】:2020-08-15 21:21:06
【问题描述】:

社区, 我有一个问题,我找不到解决方案。我有一个网站正在运行,您可以在其中打开东西。您可以使用路径:https://localhost/open/index.htmp?id=1234(或 URI 中没有 index.html)。我想在https://localhost/open/1234的整个路径中打开它,所以我设置了一个NGINX rewrite-rule:

rewrite "^/open/(?!\?id=.*)(.{1,})$" /open?id=$1 permanent; (I also tried last instead of permanent)

这条规则,多次重定向我,所以我的路径看起来像/open/?id=&id=&id=&id=&id=1234 我也试过了:

try_files $uri $uri/ /?id=$request_uri

他们都没有工作。我做错了什么?

【问题讨论】:

    标签: regex nginx url-rewriting


    【解决方案1】:

    你不需要也不能在rewrite指令中使用(?!\?id=.*)部分,因为Nginx在匹配locationtry_filesrewrite指令时使用normalised URI

    您可以将正则表达式集中到仅匹配数字 ID,例如:

    rewrite ^/open/(\d+)$ /open/?id=$1 permanent;
    

    正则表达式运算符+{1,}相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-25
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多