【问题标题】:Nginx rewrite last fragment of urlNginx 重写 url 的最后一个片段
【发布时间】:2018-06-20 10:03:15
【问题描述】:

我想将Nginx 中的 url 'https://www.example.com/knowledges/qa/123456' 转换为 'https://www.example.com/knowledges/qa.html?id=123456'。

我是 Nginx 新手,请尝试以下方法:

root   /opt/statics;

location / {            
   index index.html;
}

location /knowledges/qa/ {
   rewrite ([^\/]+)\/?$ /knowledges/qa.html?questionId=$1 last;
}

正则表达式/([^\/]+)\/?$/g可以匹配url最后一个片段的组(即123456)。

== 编辑 ==

添加最后一个斜杠/knowledges/qa/ 有效。 (否则死循环)

然后,新的问题出现了。在qa.html 中检索文档位置:

document.location.href // https://www.example.com/knowledges/qa/123456

不是预期的https://www.example.com/knowledges/qa.html?id=123456

【问题讨论】:

  • 日志是怎么说的?
  • 死循环。查看更新。
  • 如果您有不同的问题,那么您应该提出一个新问题

标签: nginx nginx-location


【解决方案1】:

我相信[^/]+ 会匹配knowledges 而不是123456。试试

rewrite ^/knowledges/qa/(\d+)$ /knowledges/qa.html?questionId=$1 last;

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多