如何防止 Apache / mod_rewrite 将路径视为同名文件

发布于 2024-12-08 00:58:29 字数 635 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小清晰的声音 2024-12-15 00:58:29

听起来您已经陷入了内容协商的陷阱;-) 正如 Apache 文档,有一个名为 MultiViews 的选项,启用该选项后,Apache 基本上会将不存在的目录名转换为其相应的文件名。

MultiViews的效果如下:如果服务器收到/some/dir/foo的请求,如果/some/dir启用了MultiViews,并且< code>/some/dir/foo 不存在,然后服务器读取目录查找名为 foo.* 的文件,并有效地伪造一个命名所有这些文件的类型映射。 ..

...目的是您可以拥有不同格式或语言的文件的多个版本,例如

/some/dir
- foo.en.gif
- foo.en.png
- foo.en.jpg
- foo.fr.gif
- foo.fr.png
- foo.fr.jpg

Apache 会根据浏览器提供的偏好选择最好的一个。

要修复此问题,您需要做的就是

Options -MultiViews

在与 /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 effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files...

The intent is that you can have several versions of a file in different formats or languages, like

/some/dir
- foo.en.gif
- foo.en.png
- foo.en.jpg
- foo.fr.gif
- foo.fr.png
- foo.fr.jpg

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

Options -MultiViews

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文