html - 如何阻止直接访问 Apache 中的特定目录和文件?

标签 html css apache .htaccess

我试图阻止直接访问某些目录,在 Ubuntu 中使用 .htaccess 的文件。我知道这个问题有很多可能重复的问题,但没有一个对我有帮助。

我的目录结构:

---- /var/www/html 
                   - login.html
                   - private (folder) 
                       -- content.html
                       -- css / style1.css , style2.css
                       -- JS / myscript1.js , myscript.js

现在我想阻止对私有(private)文件夹的直接访问。喜欢

10.0.0.1/private - Block or Password required
10.0.0.1/private/css - Block or Password required
10.0.0.1/private/js - Block or Password required

但我需要为 login.html 访问 css ,js 。所以我想允许 login.html 访问这些私有(private)文件夹。

喜欢:

 10.0.0.1/index.html - Allow 
 calling content.html via 10.0.0.1/index.html  - Allow 
 request private/css/style1.css via 10.0.0.1/index.html - Allow 
 request private/css/style2.css via 10.0.0.1/index.html - Allow
 request private/JS/myscript1.js via 10.0.0.1/index.html - Allow 
 request private/JS/myscript2.js via 10.0.0.1/index.html - Allow 

我这样写是为了阻止私有(private)文件夹访问:

<Directory /var/www/html/private/>
     AuthType Basic
     AuthName "Restricted Content"
     AuthUserFile /etc/apache2/.htpasswd
     Require valid-user
</Directory>

当我访问 10.0.0.1/private 时,这工作正常。但是当 index.html 请求 css , js 时,这会产生问题。

我如何处理 apache.conf.htacess ?我怎么写规则? 有什么建议 ?

最佳答案

在您要允许的每个文件夹中创建 .htaccess 文件(例如:private/css/.htaccess)

引用:http://httpd.apache.org/docs/2.2/mod/core.html#require

Satisfy Any
Order Allow,Deny
Allow from all

编辑:

Removing controls in subdirectories

The following example shows how to use the Satisfy directive to disable access controls in a subdirectory of a protected directory. This technique should be used with caution, because it will also disable any access controls imposed by mod_authz_host.

<Directory /path/to/protected/>
Require user david
</Directory>
<Directory /path/to/protected/unprotected>
# All access controls and authentication are disabled
# in this directory
Satisfy Any
Allow from all
</Directory>

关于html - 如何阻止直接访问 Apache 中的特定目录和文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40672208/

相关文章:

php - 使用简单的 php 'crashes' 服务器的动态 html 选项

java - Apache Tomcat 7 不接受连接

html - 将复选框输入和 <p> 标记对齐在同一行

javascript - 使用 Javascript 和 polymer 将 display none 更改为 block

javascript - php 生成 html 按钮来运行 javascript

html - 使用CSS创建半垂直椭圆

php - 如何在脚本中验证 CSS?

php - 在没有扩展的情况下运行 PHP 页面

apache - WAMP 不会变绿。和 VCRUNTIME140.dll 错误

javascript - 如何使用两个按钮调用和隐藏列表 - 使用 Javascript HTML CSS?