nginx - Nginx 重写 url 的最后一个片段
问题描述
我想将 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;
推荐阅读
- javascript - Nodejs:使用'this'的clearInterval()
- javascript - Jquery Autocomplete 未将所选值传递给部分视图
- amazon-ec2 - 来自 DHCP 客户端的 AWS EC2 连接
- angular - 识别 Angular Material 自动完成组件中的选定选项
- html - HTML CSS FLEX 问题,div 没有正确居中
- c - 如何使用“%s”说明符打印 unicode 字符串?
- java - Adding custom ValidationInterceptor using @EnableWs will cause to stop working aop on my @Configuration bean
- python - 避免在 autograd 中分配数组
- android - 如何在 Windows 上看到 React Native 输出?
- string - 需要替换一个比原来长的十六进制值