【问题标题】:Deny access to other files than / and /index.php with .htaccess使用 .htaccess 拒绝访问除 / 和 /index.php 以外的其他文件
【发布时间】: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 可能是个好主意,你能把它作为答案发布吗? (带有标志的细节总是很棘手,所以答案会很有趣)。

标签: php apache .htaccess


【解决方案1】:

也许更容易通过 mod_rewrite 处理整个事情 - 一个 RewriteCond 来检查最初请求的内容是否只是 //index.php,然后依赖于一个规则,它只使用 F 标志来回答 403禁止。否则,您已经存在的将所有其他内容重写到 index.php 的规则,可以在之后完成它的工作。

RewriteCond %{REQUEST_URI} ^(/|/index\.php)$
RewriteRule . - [F,L]
RewriteRule . index.php [QSA,L]

(目前只能通过https://htaccess.madewithlove.be/ 进行测试,这并不总是100% 匹配真实行为,但我认为这应该可以完成这项工作。)

【讨论】:

    猜你喜欢
    • 2012-10-26
    • 1970-01-01
    • 2014-12-31
    • 2010-11-23
    • 1970-01-01
    • 2011-03-25
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多