【问题标题】:How can i block direct access of particular directory , files in Apache ?如何阻止对 Apache 中特定目录、文件的直接访问?
【发布时间】:2016-11-18 08:13:08
【问题描述】:

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

我的目录结构:

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

现在我想阻止对私人文件夹的直接访问。喜欢

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

但我需要访问 css ,js 以获得 login.html 。所以我想允许login.html 访问这些私人文件夹。

喜欢:

 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 

我这样写是为了阻止私人文件夹访问:

<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 ?我该如何写规则? 有什么建议么 ?

【问题讨论】:

    标签: html css apache .htaccess


    【解决方案1】:

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

    参考:http://httpd.apache.org/docs/2.2/mod/core.html#require

    Satisfy Any
    Order Allow,Deny
    Allow from all
    

    编辑:

    删除子目录中的控件

    下面的例子展示了如何使用 Satisfy 指令来 禁用受保护目录的子目录中的访问控制。 应谨慎使用此技术,因为它还会 禁用 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>
    

    【讨论】:

    • 那么我是否也应该保留现有的/var/www/html/private/ 规则?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    • 2014-03-31
    • 2011-09-14
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    相关资源
    最近更新 更多