【问题标题】:Nginx - remove trailing slash if it's not a directoryNginx - 如果它不是目录,则删除尾部斜杠
【发布时间】:2018-04-11 16:05:10
【问题描述】:

我对重定向没有任何问题,但是当我取消注释“如果文件夹不存在”检查的 2 行时,我在任何页面上都会收到 404 错误。而且我不明白为什么,这是基本功能。

我需要将所有带有斜杠的请求重定向到没有它的 url。唯一的例外是存在具有该 url 的文件夹时。

location / {
  #if (!-d $request_filename) {
    rewrite ^/(.*)/$ /$1 permanent;
  #}

  index  index.php index.html index.htm;
  try_files $uri $uri/ /index.php?q=$request_uri;
}

【问题讨论】:

    标签: nginx


    【解决方案1】:

    您应该阅读this caution 以了解if 的使用。

    您可以尝试另一种方法,并且仅在 try_files 处理了它可以处理的 URI 后重写:

    location {
        index index.php index.html index.htm;
        try_files $uri $uri/ @rewrite;
    }
    location @rewrite {
        rewrite ^/(.*)/$ /$1 permanent;
        rewrite ^ /index.php?q=$request_uri last;
    }
    

    请参阅this document 了解更多信息。

    【讨论】:

      猜你喜欢
      • 2012-02-03
      • 1970-01-01
      • 1970-01-01
      • 2019-05-07
      • 1970-01-01
      • 1970-01-01
      • 2019-05-19
      • 2020-08-29
      相关资源
      最近更新 更多