简体   繁体   English

Nginx重写URL的最后一个片段

[英]Nginx rewrite last fragment of url

I want to transform the url ' https://www.example.com/knowledges/qa/123456 ' to ' https://www.example.com/knowledges/qa.html?id=123456 ' in Nginx . 我要的网址“变换https://www.example.com/knowledges/qa/123456 ”到“ https://www.example.com/knowledges/qa.html?id=123456在” Nginx

I'm new to Nginx and try the follw: 我是Nginx的新手,可以尝试以下方法:

root   /opt/statics;

location / {            
   index index.html;
}

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

The regex /([^\\/]+)\\/?$/g can match group of the last fragment of url(ie 123456). 正则表达式/([^\\/]+)\\/?$/g可以匹配url的最后一个片段的组(即123456)。

== Edit == ==编辑==

Add the last slash /knowledges/qa/ works. 添加最后一个斜杠/knowledges/qa/ (otherwise dead cycle) (否则死循环)

Then, the new question occurs. 然后,出现新问题。 In qa.html retrive the document location: qa.html检索文档位置:

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

Not the https://www.example.com/knowledges/qa.html?id=123456 as expected? 不是像预期的那样https://www.example.com/knowledges/qa.html?id=123456吗?

I believe [^/]+ would match knowledges not 123456 . 我相信[^/]+可以匹配的knowledges不是123456 Try 尝试

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

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

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