[英]Page 403 Forbidden for root directory for Deny from all in .htaccess
我在根目錄中有下一個.htaccess
RewriteEngine on
RewriteRule ^$ index.php [L]
Order Deny,Allow
Deny from all
<Files index.php>
Allow from all
</Files>
並為www.example.com
獲取 Page 403 Forbidden 而不是www.example.com/index.php
。
URL www.example.com/index.php
可用。
關閉對根目錄中所有文件的訪問。 這些文件由腳本生成,文件名未知。
如何解決?
<Files index.php> Allow from all </Files>
請嘗試以下操作:
<FilesMatch "^(index\.php)?$">
Allow from all
</FilesMatch>
更新:添加了丟失的錨點!
(盡管我假設您使用的是 Apache 2.4,所以您應該使用相應的Require
指令而不是Order
、 Deny
和Allow
。)
或者,將所有現有指令替換為以下內容:
DirectoryIndex index.php
RewriteEngine On
RewriteRule !^(index\.php)?$ - [F]
這允許訪問example.com/
和example.com/index.php
。 要阻止對index.php
的直接訪問,請嘗試以下操作:
RewriteRule ^[^/]+$ - [F]
mod_dir(即“DirectoryIndex”)在 mod_rewrite 之后處理。
RewriteRule ^$ index.php [L]
這條規則是多余的,它應該由DirectoryIndex
來處理。
更新:
RewriteRule.^(index?php),$ - [F] 有效。 但是我為第二個文件 index2.php 添加了 RewriteRule?^(index2.php).$ - [F] 並且它不起作用...我收到www.example.com/index2.php..的 403 錯誤。 我需要訪問多個文件
通過添加另一條規則,它最終會阻止這兩個 URL。 因為一個或另一個規則將永遠成功。
您可以在單個規則中使用正則表達式交替。 例如:
RewriteRule !^(index\.php|index2\.php)?$ - [F]
可以在上面的<FilesMatch>
容器中使用相同的正則表達式。
或者,如果您有很多這樣的異常,那么使用多個條件可能會更易讀。 例如:
RewriteCond %{REQUEST_URI} !=index.php
RewriteCond %{REQUEST_URI} !=index2.php
RewriteCond %{REQUEST_URI} !=index3.php
RewriteRule !^$ - [F]
但是請注意,與您的原始規則一樣,這也會阻止“子目錄”中的 URL,而不僅僅是根目錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.