【发布时间】:2012-08-20 07:21:06
【问题描述】:
我想拒绝目录中的所有文件,但 index.php(作为默认页面)。
这个解决方案几乎可以工作:
Deny from all
<Files index.php>
Order Allow,Deny
Allow from all
</Files>
唯一的问题:'upload/index.php' 现在可以访问了,但 '/upload/' 不是。如何使用 htaccess 允许默认页面?
【问题讨论】:
我想拒绝目录中的所有文件,但 index.php(作为默认页面)。
这个解决方案几乎可以工作:
Deny from all
<Files index.php>
Order Allow,Deny
Allow from all
</Files>
唯一的问题:'upload/index.php' 现在可以访问了,但 '/upload/' 不是。如何使用 htaccess 允许默认页面?
【问题讨论】:
您可以尝试使用 mod_rewrite,将您拥有的替换为:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(index.php)?$
RewriteRul ^ - [L,F]
【讨论】:
您的问题是,正如您可能已经发现的那样,您拒绝所有的东西,然后允许 URI 'index.php',但不允许 URI '/' - 即使 '/' 得到重定向到幕后的 index.php,它仍然是一个不同的 URI,因此它也应该被允许。
最简单的方法是使用FilesMatch 指令,如下所示:
order allow,deny
<FilesMatch "^(index\.php)?$">
allow from all
</FilesMatch>
正则表达式^(index\.php)?$ 表示“index.php 或无”。
【讨论】: