![](/img/trans.png)
[英]IIS Url Rewite: Add Trailing Slash, Preserve Anchors and Query Strings
[英]Nginx rewrite: add trailing slash, preserve anchors and query strings
我需要确保给定站点上的所有永久链接都以尾部斜杠结尾。 也就是说,任何引用与实际单个文件不对应的位置的URL。 我还需要保留随URL传递的任何查询字符串和/或锚点。
假设我在以下位置有一个页面:
example.com/about/
如果我收到以下请求,我希望它们重写如下所示:
example.com/about
> example.com/about/
example.com/about?src=fb
> example.com/about/?src=fb
example.com/about#contact
> example.com/about/#contact
example.com/about#contact?src=fb
> example.com/about/#contact?src=fb
但是 ,我想确保我不会重写任何实际的文件路径 - 任何具有文件扩展名的文件。
这是我到目前为止提出的正则表达式,它只解决了排除真实文件路径的问题,并在字符串末尾没有一个时添加了一个尾部斜杠:
^([^\.]*$(?<!\/))
我还没有弄清楚如何确定当存在锚点或查询字符串时是否存在尾部斜杠,并且一旦确定如何分别捕获应该在尾部斜杠之前和之后的部分以便组装最后的重写。
事实证明,我提出的正则表达式实际上解决了我所有的重写需求。 这是我的Nginx server
配置的最终结果:
location / {
try_files $uri $uri/ @rewrites;
}
# Rewrite rules to sanitize and canonicalize URLs
location @rewrites {
rewrite ^([^\.]*$(?<!\/)) $1/ last;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.