Mod重写 - 阻止访问dir,但某些文件除外

时间:2013-11-11 11:50:31

标签: regex apache .htaccess mod-rewrite conditional-statements

目前我有以下内容限制直接访问脚本,但有三种情况除外:

RewriteEngine on
RewriteCond %{REQUEST_URI} !read_dir [NC]
RewriteCond %{REQUEST_URI} !get_ac_options [NC]
RewriteCond %{REQUEST_URI} !view_log [NC]
RewriteRule ^inc/php /cms_spider3?script_access_attempt [NC]

前两个条件有效(即我可以同时访问read_dir.php和get_ac_options.php)但忽略第三个规则

也就是说,访问

/my/server/view_log.php

...总是会导致重定向。 view_log.php与另外两个之间的唯一区别是,其他两个总是通过AJAX访问,而view_log.php直接在浏览器中访问。我无法想象这会产生什么影响,但我想我会提到它。

我不是mod-rewrite的专家,所以我可以更好地编写完整的内容。 (我甚至不确定[NC]做了什么 - 我只是保留了一些示例代码。)

1 个答案:

答案 0 :(得分:1)

  

it's possible the entirety of what I have could be better written.

您的规则可以更好地重写为:

RewriteEngine on

RewriteCond %{REQUEST_URI} !(read_dir|get_ac_options|view_log) [NC]
RewriteRule ^inc/php /cms_spider3?script_access_attempt [L,NC]

NC用于No Case(忽略大小写匹配) L代表最后一条规则

参考:Apache mod_rewrite Introduction