如何拒絕列出父/根檔案的內容,但允許訪問所有子檔案夾及其檔案?例子
**-Root Folder (Do not access)**
-index.php (cannot be accessed)
-demo.php (cannot be accessed)
-.htaccess (for achieving this)
**-Subfolder-1 (Grant access all files and folders)**
-index.php
-example.php
-Subfolder-1 -Subfolder
**-Subfolder-2 (Grant access all files and folders)**
-index.php
-example.php
-Subfolder-2 -Subfolder
uj5u.com熱心網友回復:
默認行為是允許訪問,因此您可以使用 mod_rewrite 阻止對根目錄中檔案的訪問。
例如:
RewriteEngine On
# Block access (403 Forbidden) to the root folder itself
RewriteRule ^$ - [F]
# 403 Forbidden for any file request in the document root
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^[^/] \.\w{2,5}$ - [F]
上面的第二條規則僅匹配對根目錄中的檔案(包括檔案擴展名)的請求。如果此請求也映射到實際檔案,則回傳“403 Forbidden”回應。如果請求未映射到真實檔案,則回傳標準的“404 Not Found”回應(默認情況下)。
但是,如果您只想為根目錄中的(“看起來像”)檔案的所有請求發送 404(或 403),那么請改用以下內容,洗掉前面的條件:
# 404 Not Found for any request that looks like a file request in the document root
# - including the root directory itself
RewriteRule ^([^/] \.\w{2,5})?$ - [R=404]
通過使整個模式可選(即通過包圍 in ^(.....)?$)它也匹配根目錄。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/495760.html
標籤:.htaccess
