【发布时间】:2020-06-05 11:52:13
【问题描述】:
我已阅读 .htaccess deny access to all except to one file 及其答案。这有效:
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)。
【问题讨论】:
-
一开始就不要在你的公共 webroot 中添加任何其他内容......
-
当然是@deceze,但更一般地说,如何允许
/index.php和/而没有其他访问权限? -
也许更容易通过 mod_rewrite 处理整个事情 - 一个 RewriteCond 来检查最初请求的内容是否只是
/或/index.php,然后依赖于仅使用 @ 的规则987654337@ 标志以 403 Forbidden 回答。 -
@CBroe 可能是个好主意,你能把它作为答案发布吗? (带有标志的细节总是很棘手,所以答案会很有趣)。