Nginx从uri中删除索引,如果不存在,请尝试使用父文件夹

我正在尝试在nginx配置中创建重写,而缺乏PCRE regex经验也无济于事。

考虑以下文件夹结构([]中的名称是文件夹)

[localhost:83]
  +- [a]
  |   |
  |   +- [b]
  |   +- index.php
  |   +- index.html
  +- [c]
  |   |
  |   +- image.jpg
  |
  +- index.php
  1. 我需要从url中删除index.php,index.html和index.htm

    http://localhost:83/a/index.php/b/c?d=e&f=g

    重定向到

    http://localhost:83/a/b/c?d=e&f=g

    http://localhost:83/index.php/b/c?d=e&f=g

    重定向到

    http://localhost:83/b/c?d=e&f=g

    我可以这样做

     if ($request_uri ~* "^(.*/)index\.(html?|php)\/?(.*)$") { 
         return 301 $1$3; 
     }  
    

    但是我不希望将301标头发送给客户端,我希望在可能的情况下可以对其进行静默处理。

  2. 下一步,我需要导航到根目录,如果找不到uri,请尝试在同一文件夹中及其父文件夹中依次访问index.phpindex.htmlindex.htm,直到到达根。我已经有

    index index.php index.html index.htm;

    在我的服务器块中,所以我只需要一个接一个地尝试文件夹,直到到达服务器的根目录,换句话说,跟随uri

    http://localhost:83/a/b/c?d=e&f=g

    应该尝试跟随网址

    http://localhost:83/a/b/c/index.php?d=e&f=g
    http://localhost:83/a/b/index.php/c?d=e&f=g
    http://localhost:83/a/index.php/b/c?d=e&f=g
    http://localhost:83/index.php/a/b/c?d=e&f=g
    

    ,因为文件夹index.php服务器中有一个a。由于我知道路径中最多有5个级别,因此可以通过以下操作来做到这一点,但是我不确定如何将路径分为多个部分

    try_files 
      $uri
      /$1/$2/$3/$4/index.php$is_args$args
      /$1/$2/$3/index.php/$4$is_args$args
      /$1/$2/index.php/$3/$4$is_args$args
      /$1/index.php/$2/$3/$4$is_args$args
      /index.php/$1/$2/$3/$4$is_args$args;
    
liu0659222 回答:Nginx从uri中删除索引,如果不存在,请尝试使用父文件夹

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/1367269.html

大家都在问