[英]Nginx: How to add a trailing slash to URL except if it starts with certain name
I'm trying to add redirects in nginx config file where if a user lands on any URL except a specific one, it will add a trailing slash after it.我正在尝试在 nginx 配置文件中添加重定向,如果用户登陆除特定 URL 之外的任何 URL,它将在其后添加一个斜杠。 Also, it shouldn't add a trailing slash if there's a
.
此外,如果有
.
in it.在里面。
Example:例子:
.
in the url) .
) I did see this already which covers most of what I need:我确实已经看到了它,它涵盖了我需要的大部分内容:
#add trailing slash to all URLs except if it contains a .
rewrite ^([^.])*[^/]$ $1/ permanent;
I also figured out how to not add a trailing slash to a specific URL by doing something like this:我还想出了如何通过执行以下操作不向特定 URL 添加尾部斜杠:
rewrite ^(?!no-trailing-slash).*[^/]$ $1/ permanent;
But I can't figure out how to combine them so that:但我无法弄清楚如何将它们组合起来:
/
/
.
.
in the URLUse this with multi-line flag
:将此与
multi-line flag
:
^(.*)(\/(?!no-trailing-slash)([^.\/])+)$
Replace any match with:将任何匹配替换为:
$1$2/
The pattern matches anything that after last /
neither has .
该模式匹配任何在 last
/
都没有.
nor start with specific pattern and add /
at the end of them.也不以特定模式开始并在其末尾添加
/
。
I actually figured it out with the help of HFZ's regex example.我实际上是在 HFZ 的正则表达式示例的帮助下弄清楚的。 I used this and it works perfect:
我使用了这个,它工作得很好:
^((?!no-trailing-slash)([^.]*[^\/]))$
and replace any match with:并将任何匹配替换为:
$1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.