如何對禁止的 Apache 目錄中的單一檔案使用基本驗證?

如何對禁止的 Apache 目錄中的單一檔案使用基本驗證?

我想允許存取目錄中被禁止的單一檔案。

這不起作用:

<VirtualHost 10.10.10.10:80>
  ServerName example.com

  DocumentRoot /var/www/html

  <Directory /var/www/html>
    Options FollowSymLinks
    AllowOverride None
    order allow,deny
    allow from all
  </Directory>

  # disallow the admin directory: 
  <Directory /var/www/html/admin>
    order allow,deny
    deny from all
  </Directory>

  # but allow this single file:: 
  <Files      /var/www/html/admin/allowed.php>
      AuthType basic
      AuthName "private area"
      AuthUserFile /home/webroot/.htusers
      Require user admin1
  </Files>

  ...
</VirtualHost>

當我訪問時,http://example.com/admin/allowed.php我收到目錄的禁止訊息http://example.com/admin/,但沒有從基本身份驗證中彈出瀏覽器登入彈出窗口,因此基本身份驗證不適用於該文件。如何為 allowed.php 設定例外?

如果不可能,也許我可以在另一個文件指令中列舉所有禁止的文件?

假設 admin/ 也包含 user.php 和 admin.php,這應該在該虛擬主機中被禁止。

編輯:我還嘗試了以下修改,嘗試遵循伊格納西奧答案的建議,得到相同的結果「禁止」:

  ...

  # disallow the admin directory: 
  <Directory /var/www/html/admin>
    order allow,deny
    deny from all
  </Directory>

  # but allow this single file:: 
  <Files      /var/www/html/admin/allowed.php>
      order allow,deny
      allow from all
      AuthType basic
      AuthName "private area"
      AuthUserFile /home/webroot/.htusers
      Require user admin1
      satisfy all
  </Files>
  ...

答案1

嘗試這個:

<Directory /var/www/html/admin>
  <Files allowed.php>
    AuthType basic
    AuthName "private area"
    AuthUserFile /home/webroot/.htusers
    Require user admin1
  </Files>
  order allow,deny
  deny from all
  satisfy any
</Directory>

嵌套在目錄中的文件將僅適用於其中,因此您的程式碼區塊的組織更加邏輯,並且我認為使用“滿足任何”將允許它們按計劃合併。我不確定它是否真的需要,所以嘗試使用和不使用滿意線...

答案2

我不確定該解決方案是否<Files xxx>真的有效,因為需要文檔頁面指出它不適用於Files

Context:    directory, .htaccess

相反,apache 文件建議為該文件建立一個單獨的目錄:

刪除子目錄中的控件

以下範例示範如何使用 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>

相關內容