简体   繁体   English

如何在不带斜杠的情况下访问目录的index.php并且不获取301重定向(NGinx)

[英]How to access directory's index.php without a trailing slash AND not get 301 redirect (NGinx)

I have been looking to rewrite my api URLs so they don't have a trailing slash. 我一直在寻找重写我的api URL,以便它们没有斜杠。 I use NGinx and not Apache. 我使用NGinx而不是Apache。 I did find this answer to the same question for an Apache server, but it is not going to work out of the box for NGinx. 我确实找到了针对Apache服务器的相同问题的答案 ,但对于NGinx来说,它开箱即用。

I ended up taking this sample Apache config and used a config rewrite service to convert the config to Nginx format. 我最终获得了该示例Apache配置,并使用配置重写服务将配置转换为Nginx格式。 It produces a mostly working, but slightly broken solution. 它产生了一个大部分有效的解决方案,但是有点破损。 It accesses the URLs like it is supposed to, but it breaks accessing resources and causes the /api/index.php to be dumped as a download when accessing a non-existent file or directory in /api/. 它按预期的那样访问URL,但是当访问/ api /中不存在的文件或目录时,它会中断访问资源并导致/api/index.php被转储为下载。

I played around with the config to produce the below config which uses the html 404 message provided to the whole server. 我尝试使用配置来产生以下配置,该配置使用提供给整个服务器的html 404消息。

NGinx Nginx的

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

My File structure is: 我的文件结构是:

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

With the new config option, this translates to these functional URLs: 使用新的config选项,它将转换为以下功能URL:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM