繁体   English   中英

Nginx重写URL的最后一个片段

[英]Nginx rewrite last fragment of url

我要的网址“变换https://www.example.com/knowledges/qa/123456 ”到“ https://www.example.com/knowledges/qa.html?id=123456在” Nginx

我是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吗?

我相信[^/]+可以匹配的knowledges不是123456 尝试

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM