简体   繁体   English

.htaccess拒绝除某些目录以外的所有目录

[英].htaccess deny all except some directories

I have a folder that I wish to deny access to, but I wish there to be a subdirectory (and all its files and any subdirectories) that is accessible. 我有一个我希望拒绝访问的文件夹,但我希望有一个可访问的子目录(及其所有文件和任何子目录)。

Sample directory structure: 示例目录结构:

/modules/
/modules/gallery/public/manifest.xml
/modules/gallery/public/js/core.js
/modules/gallery/public/css/master.css
/modules/news/public/images/status.png
/modules/news/public/css/style.css

The .htaccess file needs to be in "modules" as its subdirectories are user provided (they are plugins to a CMS), each user provided folder might have a "public" directory and only files and folders in "public" should be accessible. .htaccess文件必须位于“模块”中,因为其子目录是用户提供的(它们是CMS的插件),每个用户提供的文件夹可能具有“公共”目录,并且只能访问“公共”目录中的文件和文件夹。

You can set an environment variable if the request contains a /public/ , doing something like this in your htaccess file in the modules directory: 如果请求包含/public/ ,则可以设置环境变量,在modules目录下的htaccess文件中执行以下操作:

SetEnvIf Request_URI /public/ ispublic=1
Order Deny,Allow
Deny from all
Allow from env=ispublic

If you want to be even more restrictive, you can tweak the /public/ regex to include depth, for example, only 1 directory deep into modules : 如果您想要更加严格,可以调整/public/ regex使其包含深度,例如,只有1个目录深入模块

SetEnvIf Request_URI ^/[^/]+/public/ ispublic=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM