【问题标题】:Preventing directory listing by redirecting?通过重定向防止目录列表?
【发布时间】:2009-08-04 18:29:03
【问题描述】:

我想使用.htaccess 来阻止目录列表。
我在 /location/ 中有页面,但我没有索引文件。所以我想重定向到 /location/about.php 例如。

有没有办法做到这一点,而无需创建 index.html 并将请求重定向到那个?

感谢您的帮助!

【问题讨论】:

    标签: apache .htaccess


    【解决方案1】:

    如果您要求使用文件代替“index.html”,请参阅“DirectoryIndex”以告诉它要使用哪些文件代替“index.html”:

    DirectoryIndex about.php index.html
    Options –Indexes
    

    ...如果您尝试将所有目录重定向到单个页面,那么我会作弊并执行以下操作,这主要会满足您的要求:

    Options +Indexes
    IndexOptions +SuppressHTMLPreamble
    IndexIgnore *
    HeaderName /includes/header.html
    ReadmeName /includes/readme.html
    

    ...并将 /includes/header.html 设置为您想要的任何消息(或包含元重定向),并将 /includes/readme.html 设置为空白。

    【讨论】:

    • 我使用了第一个示例。但是,包括 Options -Indexes 会导致内部服务器错误。所以我只是使用 DirectoryIndex 来替换默认的 .html 然后将其反映在每个目录中。谢谢!
    【解决方案2】:

    我知道这是一个较旧的线程,但我只需要实现类似的东西并按照以下方式进行:

    Options -Indexes
    ErrorDocument 403 /location/about.php 
    

    基本上,Options -Indexes 会阻止目录列表,ErrorDocument 403 会重写默认的 403 消息。

    【讨论】:

    • 请注意,这可能会导致一些意想不到的副作用 - 403 响应不会被许多(大多数?)搜索引擎索引
    • 而 403 是“禁止访问” - 你想要 404 吗?
    【解决方案3】:

    更改 DirectoryIndexOptions 有效,但使用 mod_alias 意味着您不必在嵌套目录中将其更改回。

    Redirect 303 /location/ /location/about.php
    

    【讨论】:

      【解决方案4】:

      这里是您需要的直接链接:

      http://www.webweaver.nu/html-tips/web-redirection.shtml

      如果您需要更高级的东西来处理参数或需要将旧 URL 重新映射到新 URL,您可以使用 URL 重写:

      http://corz.org/serv/tricks/htaccess2.php

      阻止目录列表(或返回空目录列表):

      http://www.besthostratings.com/articles/prevent-directory-listing.html

      【讨论】:

      • 我不想重定向页面,我想将目录重定向到页面。但是,如果我重定向整个目录,则该目录中的每个页面都将指向重定向的页面。或者我应该相信。
      猜你喜欢
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      • 2015-12-01
      • 1970-01-01
      • 2019-12-13
      • 1970-01-01
      相关资源
      最近更新 更多