public_html 上存在文件时 apache $1 失败

apache $1 failed when file exist on public_html

我有一个带有简单重写规则的简单 htaccess

AddDefaultCharset UTF-8

#
# page.php
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule  ^page/([^.]+)?$ page.php?slug= [L,NC,QSA]

我长期使用这个规则显示静态页面

最近几个月这个简单的规则在某些服务器(cPanel 和 VirtualMin)中不起作用,并且不明白问题出在哪里。

规则匹配并执行位于 public_html 中的 page.php 脚本,但 slug 变量为空。

我发现如果我将 page.php 重命名为 _page.php 它工作正常。

更清楚
如果粗体匹配,我就没有 slug

/([^.]+)?$.php?slug=$1

如果我使用

_page/([^.]+)?$ page.php?slug=
page/([^.]+)?$ _page.php?slug=
page/([^.]+)?$ page99.php?slug=

一切正常。

我当然有和其他有效的规则fine.Only这条规则失败了。

感谢任何帮助

看起来可能在这些服务器上启用了 MultiViews 但它无法正常工作(即缺少 slug URL 参数)。

.htaccess 文件的顶部禁用多视图:

Options -MultiViews

MultiViews 在默认的 Apache 安装中被禁用,但是,一些共享主机出于某种原因确实在服务器配置中启用了它。

MultiViews 基本上可以开箱即用地启用无扩展 URLs。当启用 MultiViews 并且您请求 /page(或 /page/<something>)时,mod_negotiation 将查找映射到相同基本名称(即 page)的资源 return 适当的 mime 类型(即 text/html)。换句话说,它将查找 page.phppage.html 等形式的文件。这发生在 之前 mod_rewrite 能够处理请求,所以URL 参数(在您的 mod_rewrite 指令中)丢失,因为 RewriteRule 没有得到处理(它不匹配)。

I have found that if i rename the page.php to _page.php its working fine.

是的,因为请求的 URL page 没有映射到物理文件的基本名称。