如何将网站“/”根页面添加到文件匹配列表中?

问题描述 投票:0回答:2

尝试在这里搜索,但似乎没有适合我的具体情况的答案。

因此,我在 .htaccess 文件中使用它来仅允许访问我网站中的多个文件:

ErrorDocument 403 /403.html

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

所以我想让用户只打开主页,即index.html,和几个txt文件,你可以从代码块中看到。如果用户打开“https://example.com/index.html”,它会完美工作,但如果用户尝试打开“https://example.com/”,他会看到 403 错误。如何将“/”添加到允许的页面?

我尝试添加以下内容:

<filesMatch "(index\.html|/|somefile\.txt|somefile2\.txt)">

但运气不佳...

.htaccess
2个回答
0
投票

不确定是否有任何方法可以用

FilesMatch
做到这一点。使用 mod_rewrite 代替的解决方案怎么样?

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(|index\.html|somefile\.txt|somefile2\.txt)$
RewriteRule . - [F]

RewriteCond 检查请求的 URI 是否不是您想要允许的 URI 之一,如果满足该条件,则以下规则将使其给出 403 禁止响应。


0
投票

这是您需要允许的“空文件”,而不是

/
(实际上是“目录”,而不是文件)。因此,从您的示例中,您可以将模式设为可选:

<FilesMatch "(index\.html|somefile\.txt|somefile2\.txt)?">

或者,在正则表达式替换中包含一个 empty 元素(而不是

/
)。例如:

<FilesMatch "(|index\.html|somefile\.txt|somefile2\.txt)">

尽管考虑将

/index.html
重定向到
/
以避免规范化(重复内容)问题。

请注意,

Order
Allow
是 Apache 2.2 指令。如果您使用的是 Apache 2.4,那么这些以前已被弃用,您应该使用等效的
Require
指令来代替。

© www.soinside.com 2019 - 2024. All rights reserved.