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;
    

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)