Luk*_*lze 28
您可以通过将以下内容添加到.htaccess文件来隐藏"父目录"链接:
IndexIgnore ..
Run Code Online (Sandbox Code Playgroud)
但这会为每个目录隐藏它.
如果您可以访问httpd-configs,则可以尝试从每个服务器上下文中删除该指令:
<DirectoryMatch "^/(?!var/www/your-root/subfolder)">
...
</DirectoryMatch>
Run Code Online (Sandbox Code Playgroud)
只要在更高的文件夹中没有IndexIgnore指令,它就应该适合您.
小智 6
我刚刚遇到了同样的问题,这是第一个搜索结果,所以我想我会分享我的解决方案。在我的情况下,我无法访问 httpd-configs,所以我采用了不同的方法。
因为我不会维护索引目录树,所以我需要一个可以仅存在于索引根中而对其子目录没有任何特定知识的解决方案。但是,它确实使用了索引根目录的绝对路径,因此如果您的父文件夹结构变化很大,它可能不适合您。
我想出的是使用 CSS URL 属性选择器来隐藏“父目录”链接,当它导致索引根的父目录时。它留下了一个空白行,但至少它不像用户在多次单击父目录时得到的“403 FORBIDDEN”错误那样令人生畏。
从理论上讲,修复应该像在索引根目录的 .htaccess 文件中添加一行一样简单:
IndexHeadInsert "<style type=\"text/css\">a[href=\"/absolute/path/to/index/root/parent/\"] {display: none;}</style>"
Run Code Online (Sandbox Code Playgroud)
但是,似乎这个简单的解决方案在 IE 中不起作用,因为 Apache 吐出的 html 的默认文档类型太旧而无法使用属性选择器。
相反,我们必须使用htaccess 文件中的HeaderName和 ReadmeName 关键字将Apache 生成的代码包含在我们自己的文档类型中。我选择了 HTML5 文档类型,尽管可能有更合适的文档类型。
.htaccess 文件:
Options +Indexes
IndexIgnore "_index_header.html" "_index_footer.html"
HeaderName "/absolute/path/to/root/index/_index_header.html"
ReadmeName "/absolute/path/to/root/index/_index_footer.html"
IndexOptions +SuppressHTMLPreamble
Run Code Online (Sandbox Code Playgroud)
_index_header.html 文件:
<!DOCTYPE html>
<html>
<head>
<title>title</title>
<style type="text/css">
a[href="/absolute/path/to/index/root/parent/"] {display: none;}
</style>
</head>
<body>
Run Code Online (Sandbox Code Playgroud)
_index_footer.html 文件:
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
(注意 CSS 选择器指向索引根的父级)
适用于我测试过的所有最新浏览器版本(FF、IE、Chrome、Safari、Opera),并且一直回到 IE 7。
此外,既然您已经经历了创建这些文件的所有麻烦,您不妨让您的索引更加时髦。mod_autoindex上的apache 文档有很多选项可以用来调整东西,除了你可以用 CSS 做的事情之外。