2016-11-18 94 views
0

我試圖阻止在Ubuntu中直接訪問某些目錄,使用.htaccess文件。我知道這個問題有很多可能的重複,但他們都沒有幫助我。如何阻止特定目錄,Apache中的文件的直接訪問?

我的目錄結構:

---- /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 ,jslogin.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?我怎麼寫規則? 有什麼建議嗎?

回答

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> 
+0

所以要我把我現有的'在/ var/WWW規則/ HTML /私營/'也? – user2986042

+0

是的。保留兩個。 – Rohit

+0

請你用規則更新答案嗎? – user2986042