我想将不带斜杠的 URL 重定向到带尾部斜杠的路径。
所以/一些-url to /一些网址/
其余的 URL,例如
- /some-url.xml
- /某个网址?
- /some-url?q=v
- /一些网址/
应该保持不变。
我找到了这篇文章https://www.ateamsystems.com/tech-blog/nginx-add-trailing-slash-with-301-redirect-without-if-statements/ https://www.ateamsystems.com/tech-blog/nginx-add-trailing-slash-with-301-redirect-without-if-statements/作者建议使用以下规则:
location ~ ^([^.\?]*[^/])$ {
try_files $uri @addslash;
}
location @addslash {
return 301 $uri/;
}
不幸的是这并没有真正起作用。因为网址/some-url?q=v被重定向到/一些网址/
您能建议如何更改正则表达式以使其正常工作吗?
这应该可以解决问题:
location / {
if ($request_uri ~ ^([^.\?]*[^/])$) {
return 301 $1/;
}
try_files $uri $uri/ /index.php$is_args$args;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)