err_too_many_redirects nginx 使用重写规则从 url 中删除斜线

err_too_many_redirects nginx when using rewrite rule for removing slash from url

我的问题与下一种情况有关:当我尝试添加规则以从 url 中删除斜杠时,我看到下一个错误代码 "err_too_many_redirects"(如果我尝试检查这种 links 像 site.com/images/ 或其他目录 link return 403 code )

location / {
        try_files $uri $uri/ /index.php?$query_string;
        rewrite ^/(.*)/$ / permanent;#remove slash
}

谁能帮我找到解决这个问题的方法?

站点正在使用 nginx + php-fpm。

您遇到以下 URL 导致 403 错误的情况,因为目录 images 实际存在:

example.com/images/

问题是由 try_files 指令中的 $uri/ 元素试图定位目录索引引起的。

通过删除该元素(和 rewrite 指令),请求的 URI 应传递给 /index.php 以作为 漂亮的 URL[=30 进行处理=].尝试:

location / {
    try_files $uri /index.php?$query_string;
}

如果您需要将 index 应用于层次结构中的某些目录,您可以明确指定规则(而不是使用 $uri/index 指令),方法是使用(例如):

location / {
    try_files $uri $uri/index.html /index.php?$query_string;
}