主页的 RewriteCond 不起作用

RewriteCond for homepage not working

我正在尝试将 RewriteRule 应用于主页。我希望以下规则只能找到主页...

RewriteCond %{REQUEST_URI} ^/$

这是行不通的。关于原因有什么想法吗?

我在 htaccess 文件中唯一做的另一件事是在上述之后的一个简单测试 RewriteRule...

RewriteRule (.*) /test.php [L,QSA]

有时,老实说我不确定为什么,您会注意到 mod_dir 在 mod_rewrite 之前 被应用到 URI。它应该是,但我注意到 ^/$ 有时工作,有时甚至 ^$ 在重写规则中工作。发生的事情是 DirectoryIndex 被应用到 URI,它变成了 /index.php 或其他什么。所以你要做的就是与之匹配。所以像这样:

RewriteCond %{REQUEST_URI} ^/(?:index\.php)$
RewriteRule (.*) /test.php [L,QSA]

或者更简单地说:

RewriteRule ^(?:index\.php)$ /test.php [L,QSA]

这样,//index.php 都会匹配。