【问题标题】:How to access directory's index.php without a trailing slash AND not get 301 redirect (NGinx)如何在没有斜杠的情况下访问目录的 index.php 并且不获得 301 重定向(NGinx)
【发布时间】:2019-02-26 19:52:46
【问题描述】:

我一直在寻求重写我的 api URL,以便它们没有尾部斜杠。我使用 NGinx 而不是 Apache。对于 Apache 服务器,我确实找到了 this answer 来解决同样的问题,但它不适用于 NGinx。

【问题讨论】:

    标签: php nginx redirect


    【解决方案1】:

    我最终选择了this sample Apache config 并使用a config rewrite service 将配置转换为 Nginx 格式。它产生了一个主要工作但略有破坏的解决方案。它按应有的方式访问 URL,但它会中断访问资源并导致 /api/index.php 在访问 /api/ 中不存在的文件或目录时被转储为下载。

    我使用配置来生成以下配置,该配置使用提供给整个服务器的 html 404 消息。

    NGinx

    location /api {
      if (-e $request_filename){
        rewrite ^/(.*[^/])$ /$1/;
      }
    }
    

    我的文件结构是:

    /api/index.php
    /api/hotbits/index.php
    /api/cryptography/index.php
    

    使用新的配置选项,这将转换为这些功能性 URL:

    /api
    /api/
    /api/hotbits
    /api/hotbits/
    /api/cryptography
    /api/cryptography/
    

    【讨论】:

      猜你喜欢
      • 2013-09-08
      • 2013-03-11
      • 2012-10-13
      • 2012-05-15
      • 2020-06-28
      • 2016-05-10
      • 1970-01-01
      • 2013-01-27
      • 1970-01-01
      相关资源
      最近更新 更多