.htaccess重写规则中有错误吗


Is there any error in .htaccess rewriting rules

我正在尝试使用Apache 中的.htaccess为我的网站创建搜索引擎友好的URL

我想将url http://localhost/simple_blog/tmp重定向到

http://localhost/simple_blog/tmp.php

这是我的改写规则

 RewriteEngine On    # Turn on the rewriting engine
 RewriteRule ^tmp/?$  tmp.php [NC,L]

当我使用时,它运行良好

http://localhost/simple_blog/tmp/

http://localhost/simple_blog/tmp

但是当我使用时

http://localhost/simple_blog/tmp.

为什么它重定向到

http://localhost/simple_blog/tmp.php

我预计它应该抛出404,因为根据regex,它应该在开头匹配'tmp',"/"是可选的,然后是字符串的结尾

正则表达式中有错误吗??

^断言字符串开头的位置,即http://...

?量词确实使/是可选的。

因此,我认为正确的正则表达式应该是tmp/?$。。。从技术上讲,这应该有效。不过,我不确定PHP是如何处理regex的。

您也可以在正则表达式的末尾尝试负面前瞻,例如tmp/?(?!'.+)$