如何在nginx中使用斜杠配置重定向到url? [英] How to configure redirects to url with trailing slash in nginx?
本文介绍了如何在nginx中使用斜杠配置重定向到url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将不带斜杠的URL重定向到带斜杠的路径. 因此,将/some-url 更改为/some-url/
I want to redirect URLs without slash to the path with trailing slash. So /some-url to /some-url/
其余网址,例如
- /some-url.xml
- /some-url?
- /some-url?q = v
- /some-url/
应该保持不重定向的状态.
Should stay without redirection.
I found this article https://www.ateamsystems.com/tech-blog/nginx-add-trailing-slash-with-301-redirect-without-if-statements/ in which author suggests to use following rule:
location ~ ^([^.\?]*[^/])$ {
try_files $uri @addslash;
}
location @addslash {
return 301 $uri/;
}
不幸的是,这实际上是行不通的.因为url /some-url?q = v 被重定向到/some-url/
Unfortunately this doesn't really work. Because url /some-url?q=v gets redirected to /some-url/
您能建议如何更改正则表达式以使其起作用吗?
Could you suggest how to change regular expression to make it work?
推荐答案
这应该可以解决问题:
location / {
if ($request_uri ~ ^([^.\?]*[^/])$) {
return 301 $1/;
}
try_files $uri $uri/ /index.php$is_args$args;
}
这篇关于如何在nginx中使用斜杠配置重定向到url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文