如何防止 Apache / mod_rewrite 将路径视为同名文件
我正在使用 WAMP 服务器,大部分配置都是开箱即用的。我无法让 mod_rewrite 在本地按预期运行(在生产服务器上一切正常)。
我有一个 PHP 文件位于: /ajax/graphs/get-graph.php
通常调用此文件的方式是通过加载的引导文件 /index.php
我的根目录有一个 .htaccess 文件,其规则如下:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L]
所以,基本上,当我的应用程序通过 AJAX 请求时,会调用 /ajax/graphs/get-graph/
它应该定向到/index.php
。
问题是,Apache/mod_rewrite 看到请求路径并直接加载 /ajax/graphs/get-graph.php
。
如何防止 Apache 假设 /ajax/graphs/get-graph/
是有效文件,因为该位置存在同名的 php 文件?
I'm using WAMP Server, mostly configured as-is out of the box. I'm having trouble getting mod_rewrite to behave as expected locally (everything works fine on a production server).
I have a PHP file located at:/ajax/graphs/get-graph.php
The way this file is normally invoked is via a bootstrap file loaded by/index.php
I have a .htaccess file at the root with the following rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L]
So, basically, when my app requests via AJAX a call to /ajax/graphs/get-graph/
it should be directed to /index.php
.
The problem is, Apache/mod_rewrite sees the request path and loads /ajax/graphs/get-graph.php
directly.
How do I prevent Apache from assuming that /ajax/graphs/get-graph/
is a valid file because a php file of the same name exists at that location?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您已经陷入了内容协商的陷阱;-) 正如 Apache 文档,有一个名为
MultiViews
的选项,启用该选项后,Apache 基本上会将不存在的目录名转换为其相应的文件名。...目的是您可以拥有不同格式或语言的文件的多个版本,例如
Apache 会根据浏览器提供的偏好选择最好的一个。
要修复此问题,您需要做的就是
在与
/ajax/graphs< 对应的
或
块中 添加指令/代码>。或者,如果您无权访问主服务器配置,则可以将其放在/ajax/graphs/.htaccess
中。It sounds like you've fallen into the trap of content negotiation ;-) As explained in the Apache documentation, there is an option called
MultiViews
which, when enabled, causes Apache to basically convert nonexistent directory names into their corresponding filenames.The intent is that you can have several versions of a file in different formats or languages, like
and Apache will choose the best one based on the preferences provided by the browser.
To fix it, all you should need to do is add the directive
in a
<Directory>
or<Location>
block corresponding to/ajax/graphs
. Or, if you don't have access to the main server configuration, you can put it in/ajax/graphs/.htaccess
.