【问题标题】:htaccess preventing index.html from loading automaticallyhtaccess 防止 index.html 自动加载
【发布时间】:2013-07-14 16:10:16
【问题描述】:

我的网站根文件夹中有以下 htaccess 文件:

# Deny access to everything by default
Order Deny,Allow
deny from all

# Allow access to html files
<Files *.html>
    allow from all
</Files>

我这样做是为了防止访问除 html 文件之外的所有内容。但是,它似乎也阻止了 index.html 自动加载(例如,导航到 http://www.website.com 失败)。但是如果我特别请求 index.html(例如http://www.website.com/index.html),我可以访问它。

我怎样才能拒绝访问除 html 文件之外的所有内容并仍然自动加载 index.html?

【问题讨论】:

  • 注意:我已经尝试添加 DirectoryIndex index.html 语句和特定的 Files 规则以允许 index.html,但这些都没有帮助。 :P

标签: apache .htaccess


【解决方案1】:

我认为您必须在 .htaccess 文件中使用 DirectoryIndex 指令:

DirectoryIndex index.html

【讨论】:

  • 不幸的是,我已经尝试过了,但它似乎并没有解决问题......该网站托管在 godaddy shared,所以我想知道他们是否有一些奇怪的配置更高造成这种情况。
【解决方案2】:

我能够添加 FilesMatch 规则来使其正常工作:

# Deny access to everything by default
Order Deny,Allow
deny from all

# Enable the redirect to index.html
<FilesMatch ^$>
    allow from all
</FilesMatch>

# Allow access to html files
<Files *.html>
    allow from all
</Files>

这实质上允许任何匹配空请求路径的东西。我认为这里的问题是在应用自动索引重定向之前,允许/拒绝规则已应用于传入请求。所以基本上http://www.website.com/ 的请求会进来,不匹配任何列入白名单的文件类型,并被所有语句中的一揽子拒绝拒绝。显然,这是在那个时候使请求短路 - 在它可以自动重定向到对 index.html 的请求之前,这是允许的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 2017-10-31
    • 2016-01-07
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 2011-11-26
    相关资源
    最近更新 更多