Apache 在 /api 请求上的奇怪行为

Apache's strange behaviour on /api request

请求我网站的任何资源,如 /profile、/profile/、/market(带或不带最后的斜杠)和 /api/ 都很好,但是当我尝试请求 /api (没有最后的斜杠)我得到 301 永久移动并重定向到 /api/?_url=api/api。 /profile 和 /api 之间的 Apache 区别在哪里以及如何修复此重定向?

我的 .htaccess 看起来像这样:

RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpeg|jpg|svg)$
RewriteRule ^(.*)$ controller.php?_url= [QSA,L]

我正在使用 Apache 2.2.27

不同之处很可能是 /api/ 是一个目录,而 mod_dir 在上述规则是 mod_rewrite 模块中的 运行 之后添加了尾部斜线。您可以通过这样的代码来控制此行为:

# turn off auto-trailing slash after a directory
DirectorySlash off
RewriteEngine on

# add a trailing slash to directories
RewriteCond %{DOCUMENT_ROOT}/ -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302,NE]    

RewriteCond %{REQUEST_URI} !\.(css|js|png|jpeg|jpg|svg)$ [NC]
RewriteRule ^(.*)$ controller.php?_url= [QSA,L]