How to prevent Apache / mod_rewrite from treating path as file with same name

我怕爱的太早我们不能终老 提交于 2019-12-05 07:20:12

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/7666487/how-to-prevent-apache-mod-rewrite-from-treating-path-as-file-with-same-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!