RewriteRule 匹配根目录?

RewriteRule match root dir?

SO 中关于用于匹配根目录请求的确切正则表达式的现有答案似乎存在一些历史性的混淆。例如:^/$、^$ 或 ^/?$

查看答案here and here。 那么,让我们澄清一下

问题:

What is currently valid, 'for 2018' way to match a request to the root dir?

单例测试(chrome)

我做了 运行 快速测试,似乎(至少 chrome)总是匹配 ^$ 而不管 url 是如何输入的,包括 /?test。最后一点让我觉得服务器配置也很重要。

RewriteRule ^/$ /index-with-slash.html [L,R=301,NE] <-- no hits
RewriteRule ^$ /index-wo-slash.html [L,R=301,NE] 

^/?$ 将匹配这两种情况。无需重新发明轮子。

如果您担心正则表达式占用大量资源,您可能无论如何都不会使用 Apache。