【发布时间】:2017-05-20 03:00:30
【问题描述】:
每当我的网络服务器中有一个文件并在我的.htaccess 中为同一路径重写规则时,该规则将被忽略并自动提供该文件。
我的网络服务器的public_html 中只有一个文件-test.php,如下所示:
<?php
echo "hello ".$_GET['action']
?>
最初我的.htaccess 是空的,所以访问http://<domain-name>/test.php?action=world 会像预期的那样回显hello world。此外,我的虚拟主机配置如此,访问/test?action=world(.htaccess 为空)也会回显 hello world。
现在我在public_html 中添加以下.htaccess:
RewriteEngine On
RewriteRule ^test/(\w+)$ test.php?action=$1 [NC,L]
我希望访问/test/world 会回显hello world,但这不会发生!我在回复中只收到hello。现在我最初的想法是mod_rewrite 没有启用,所以为了测试我将.htaccess 更改为:
RewriteEngine On
RewriteRule ^testmod/(\w+)$ test.php?action=$1 [NC,L]
令我惊讶的是,现在访问/testmod/world 实际上与hello world 相呼应!这表明mod_rewrite确实可用,并确认当请求的url实际存在文件时重写规则被忽略。
我的心智模型是 Apache 遍历 .htaccess 中的规则,并且对于匹配请求规则的任何重写规则,Apache 在内部重定向到规则中指定的路径。但这确实发生在这里。看起来首先它检查是否存在请求的 url 的任何文件,如果不存在,则查看重写规则。或者这里发生了完全不同的事情,可能是因为我的虚拟主机的配置? (仅供参考,我使用Bluehost 作为我的虚拟主机)。
【问题讨论】:
标签: php .htaccess mod-rewrite url-rewriting