【发布时间】:2021-11-27 02:45:09
【问题描述】:
我正在尝试在 nginx 配置文件中添加重定向,如果用户登陆除特定 URL 之外的任何 URL,它将在其后添加一个斜杠。此外,如果其中有 .,则不应添加尾部斜杠。
例子:
- www.site.com/foo => www.site.com/foo/
- www.site.com/foo/baz => www.site.com/foo/baz/
-
www.site.com/foo.xml => www.site.com/foo.xml(没有尾部斜杠,因为网址中有
.) - www.site.com/no-trailing-slash => www.site.com/no-trailing-slash(没有专门针对此 URL 的斜杠)
我确实已经看到了这个,它涵盖了我需要的大部分内容:
#add trailing slash to all URLs except if it contains a .
rewrite ^([^.])*[^/]$ $1/ permanent;
我还想出了如何通过执行以下操作不向特定 URL 添加斜杠:
rewrite ^(?!no-trailing-slash).*[^/]$ $1/ permanent;
但我不知道如何将它们组合起来:
- 所有重定向都将添加一个尾随
/ - 除非他们的网址中有
. - 并且不以 `/no-trailing-slash URL 开头
【问题讨论】:
标签: regex .htaccess nginx redirect