mod_rewrite & .htaccess 无法在 OS X Yosemite 中传递变量

mod_rewrite & .htaccess can't pass variables in OS X Yosemite

我有一个简单的重写规则,按预期工作正常:

RewriteRule ^user/(\w+)/?$  user.php?id=

我有一个 php 文件:

<?php echo $_GET['id']; ?>

这适用于以下 URL,并且 id 可以顺利通过:

http://localhost/user?id=JoeUser

我知道 mod_rewrite 正在工作,因为这个 url 也将我发送到 user.php:

http://localhost/user/JoeUser

但 JoeUser 从未真正传递给 $1。我以前从未见过这种行为。这是 OS X 中的错误,还是我遗漏了什么?

Is it a bug in OS X?

不,这不是错误。这是默认情况下在 Apache 配置中启用的 MultiViews 选项的效果。选项 MultiViewsApache's content negotiation module 使用,它在 之前 mod_rewrite 运行并使 Apache 服务器匹配文件的扩展名。所以 /file 可以在 URL 中,但它将服务于 /file.php.

要关闭它,请使用:

Options -MultiViews
RewriteEngine On

RewriteRule ^user/(\w+)/?$ user.php?id= [L,QSA,NC]