htaccess 显示 "Not Found" 如果目录不存在

htaccess shows "Not Found" if directory does not exist

我想将匹配 <domain>/something/mypage.htm 的所有内容重写为 something.php?pid=mypage,所以我写了以下 .htaccess 文件:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).htm$ .php?pid=
DirectoryIndex home.php

在我的旧虚拟主机提供商上,规则按预期工作,但在新提供商上,当我尝试打开 <domain>/something/index.htm 或类似内容时,它显示“未找到”。但是当我创建一个名为 something 的目录时,重写规则按预期工作。

有人知道这里出了什么问题吗?

看起来 MultiViews 可能在新服务器上启用了,这将与您的 mod_rewrite 指令冲突。

启用 MultiViews 并请求 /something/mypage.htm(当 /something.php 作为物理文件存在时),mod_negotiation 将发出 /something.php/mypage.htm 的子请求(在您的 mod_rewrite 指令能够处理请求之前,/something.php 的路径信息为 /mypage.htm),因此没有传递 pid 参数。 (您的指令与第一个路径段中的点不匹配,因此 MultiViews 的子请求不匹配。)

404 将来自 Apache,如果设置了 AcceptPathInfo Off(可能在服务器配置中),或者来自您的脚本,因为未传递 pid 参数。

当您创建名为 something 的子目录时,MultiViews 不会重写请求,因为它已经匹配了 something,即。该名称的目录。

您需要确保 MultiViews.htaccess 文件的顶部被禁用:

Options -MultiViews

注意:默认情况下,Apache 上未启用 MultiViews,但某些共享网络主机出于某种原因确实启用了它。它使无扩展名的 URL“神奇地”开箱即用,但如果您没有预料到它,实际上会导致更多问题。