.htaccess 阻止访问子文件夹

.htaccess blocks access to subfolders

目标:我想通过使用 .htaccess 来防止目录遍历(尽管这不是很好的做法)。

情况:我在 .htaccess 中有这一行:Options All -Indexes

果然,允许输入 localhost/rootfolder/page.php,但输入 localhost/rootfolder/ returns 一个 403 Forbidden 页面(预期)。

问题:但是,我怀疑由于 .htaccess 中的 Options All -Indexes,我什至无法从子文件夹中提取文件。

例如,我有一个文件路径为 rootfolder/subfolder/image.png 的图像,使用 <img src=./subfolder/image.png> 显示。出现的图像 returns a "broken image".

右键单击 > 查看图像使用附加到本地主机的文件路径打开图像,即 localhost/rootfolder/subfolder/image.png、returns 和 403 Forbidden 页面。

问题:如何使用 .htaccess 来阻止访问目录查看,但仍允许访问子文件夹中的单个文件?

您可以在 htaccess 中使用以下行:

RedirectMatch 403 ^/folder/?$

这将禁止对 /folder 的请求,但您可以访问您的文件和子文件夹。