目录索引页在 htaccess 之前

Directory index page precedes htaccess

我想要的是,只有当我的根域被访问意味着 URI 为空时,它才会在没有重定向的情况下被重写,否则如果不访问文件或文件夹则重写为 index.php。我的 .htaccess 看起来像这样:

RewriteEngine On
RewriteBase /
RewriteRule ^/?$ /mypages/landing_page.html [QSA,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

它在某些服务器上按预期工作,但在另一台服务器上它只有在文件夹中没有 index.php 文件时才能工作。类似于 htaccess 规则之前的目录索引?如果我删除 index.php 或将其重命名为 index2.php 并相应地更改我的 htaccess 规则,则重写会按预期工作。

有什么想法可能导致服务器出现这样的行为吗?

Apache 2.4.2 中引入了一个错误 (#53929),它具有与您描述的相同症状:

"The issue is caused by the DirectoryIndex directive. mod_dir is not respecting the result of the rewrite execution. If DirectoryIndex is set to disabled, it starts working correctly."

此问题已在 Apache 2.4.9 中修复。

如果您有混合环境(即 Apache 2.4.2 之前和 post 版本),这也可以解释为什么您的 Rewrite 在某些服务器上正常工作,但在其他服务器上却不能。

如果升级不是一个选项,您也可以使用条件 SetHandler(基于您的 RewriteRule 中设置的环境变量)来解决该问题。这将为那些请求禁用 DirectoryIndex。

错误本身(和解决方法示例)记录在以下内容中 link:

https://bz.apache.org/bugzilla/show_bug.cgi?id=53929