You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将网站根路径"/"添加到filesMatch允许列表中?

解决.htaccess根目录访问403的问题

当你访问https://example.com/出现403错误,是因为FilesMatch规则只匹配文件名,而根目录请求(/)属于目录访问,不会被FilesMatch捕获——你直接把/加到正则里没用,因为它不是文件名。

下面给你几种可行的解决办法:

方法1:直接允许根目录访问

在现有规则前添加目录权限配置,开放根目录的访问:

ErrorDocument 403 /403.html

DirectoryIndex index.html

# 允许访问网站根目录
<Directory "/">
    Allow from all
</Directory>

Order Allow,Deny
<filesMatch "(index\.html|somefile\.txt|somefile2\.txt)">
Allow from all
</FilesMatch>

注意:如果网站有其他子目录,这种方法会默认开放子目录的访问权限,需要额外添加规则限制子目录。

方法2:用mod_rewrite精准控制

改用重写规则匹配请求URI,灵活性更强:

ErrorDocument 403 /403.html
DirectoryIndex index.html

RewriteEngine On
# 只允许根目录、index.html和指定的txt文件访问
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/index\.html$
RewriteCond %{REQUEST_URI} !^/somefile\.txt$
RewriteCond %{REQUEST_URI} !^/somefile2\.txt$
# 其他所有请求返回403
RewriteRule ^ - [R=403,L]

这种方式直接针对请求路径做判断,根目录/会被第一个条件排除,自然允许访问,其他未匹配的请求全部拒绝。

方法3:用SetEnvIf标记允许的请求

先拒绝所有访问,再标记符合条件的请求为允许:

ErrorDocument 403 /403.html
DirectoryIndex index.html

Order Deny,Allow
Deny from all
# 标记根目录、index.html和指定txt文件为允许访问
SetEnvIf Request_URI "^/$" allowed
SetEnvIf Request_URI "^/index\.html$" allowed
SetEnvIf Request_URI "^/somefile\.txt$" allowed
SetEnvIf Request_URI "^/somefile2\.txt$" allowed
Allow from env=allowed

这种逻辑更清晰,先锁死所有访问,再开放指定路径,安全性更高。

内容的提问来源于stack exchange,提问作者Andrew Poroshenko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 09:52:53