[英]Deny access to other files than / and /index.php with .htaccess
我已閱讀.htaccess 拒絕訪問除一個文件及其答案之外的所有文件。 這有效:
Order Allow, Deny
<FilesMatch "^index\.php$">
Allow from all
</FilesMatch>
但只接受http://example.com/index.php
。 問題是http://example.com/
被拒絕。
問題:只允許/index.php
和/
的最干凈的方法是什么? (都使用 index.php)?
我認為這不是最佳選擇:
Order Allow, Deny
<FilesMatch "^index\.php$">
Allow from all
</FilesMatch>
RewriteRule .* index.php [QSA,L]
因為它混合了允許/拒絕/重寫規則。
PS:在我的真實代碼中,我只想接受/
, /index.php
,還有/test.php
,但不能訪問其他文件(例如/blah.php
/foo.log
)
也許更容易通過 mod_rewrite 處理整個事情 - 一個 RewriteCond 來檢查最初請求的內容是否只是/
或/index.php
,然后依賴於一個簡單地使用 F 標志來回答 403 Forbidden 的規則。 否則,您已經存在的規則將其他所有內容重寫為 index.php,之后可以完成它的工作。
RewriteCond %{REQUEST_URI} ^(/|/index\.php)$
RewriteRule . - [F,L]
RewriteRule . index.php [QSA,L]
(現在只能通過https://htaccess.madewithlove.be/進行測試,這不能總是 100% 匹配真實行為,但我認為這應該可以完成工作。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.