【发布时间】:2011-03-25 08:47:57
【问题描述】:
如何拒绝访问整个文件夹和子文件夹,但一个文件除外? 该文件是:toon.php,并且位于同一个文件夹中。
【问题讨论】:
标签: .htaccess
如何拒绝访问整个文件夹和子文件夹,但一个文件除外? 该文件是:toon.php,并且位于同一个文件夹中。
【问题讨论】:
标签: .htaccess
Order Allow,Deny
<FilesMatch "^toon\.php$">
Allow from all
</FilesMatch>
这可能是你能得到的最有效的方法。
【讨论】:
AuthGroupFile /dev/null
AuthName "Private Area"
AuthType Basic
AuthUserFile .htpasswd
require valid-user
<Files "toon.php">
Allow from all
Satisfy Any
</Files>
为我工作。不要忘记配置 .htpasswd 文件。
【讨论】:
AllowOverride AuthConfig Limit
Deny From All
<FilesMatch "^toon\.php$">
Allow From All
</FilesMatch>
为我工作...
【讨论】:
Deny From All 是Allow From All 更清楚。显式优于隐式。对此答案 +1。
您可能想要使用<Directory> 和<Files> 指令。您可以在此处查看文档:
http://httpd.apache.org/docs/1.3/mod/core.html#directory
http://httpd.apache.org/docs/1.3/mod/core.html#files
简而言之,你想要这样的东西:
<Directory /folder/without/access >
Order Deny,Allow
Deny from All
</Directory>
<Files "filename">
Order Deny,Allow
Allow from All
</Files>
【讨论】:
如果像我一样,您正在寻找一种方法来对除单个文件之外的整个站点/文件夹进行身份验证,您会发现这种方法效果很好:
#allows a single uri through the .htaccess password protection
SetEnvIf Request_URI "/testing_uri$" test_uri
AuthName "Restricted Area"
AuthType Basic
AuthUserFile /path/to/your/.htpasswd
AuthGroupFile /
Require valid-user
#Allow valid-user
Deny from all
Allow from env=test_uri
Satisfy any
示例来自:this site
【讨论】:
将以下规则添加到根目录下的 htaccess 文件中
RewriteEngine on
RewriteRule !toon\.php$ - [F]
这将拒绝访问除 toon.php 之外的所有文件和文件夹。
【讨论】:
WordPress 管理中有一个相关场景,您可能希望限制对特定 IP 的访问,但保留对插件使用的某些特定文件的访问权限:
## /wp-admin/.htaccess ##
Deny from all
allow from 88.222.123.88 #Home
allow from 88.111.211.77 #Office
<Files "admin-ajax.php">
Allow from All
</Files>
【讨论】:
使用 HTTP 密码的专用区域的 WordPress 特定配置:
AuthName "Private Area"
AuthType Basic
AuthUserFile .htpasswd
require valid-user
<Files "admin-ajax.php">
Allow from all
Satisfy Any
</Files>
【讨论】: