【问题标题】:.htaccess on 000webhost options [closed]000webhost 选项上的 .htaccess [关闭]
【发布时间】:2021-02-12 16:49:32
【问题描述】:

我正在尝试修改我在 000webhost 中的 .htaccess 文件,以便仅为特定文件夹禁用 Indexes 选项(因为它默认启用)。

public_html/ 文件夹中有以下所有文件:

  • nas/(空目录)
  • test/(空目录)
  • .htaccess

我想禁用test/ 文件夹中的索引选项,但在nas/ 上保持启用该选项。我曾尝试使用以下 Directory 容器,但它会使网站崩溃:

<Directory /public_html/test/>
    Options -Indexes
</Directory>

我一直在尝试许多不同的路径(因为据我所知,000webhost 使用虚拟路由器,但我找不到方法)。

非常感谢!

【问题讨论】:

    标签: apache .htaccess directory


    【解决方案1】:
    <Directory /public_html/nas/>
        Options -Indexes
    </Directory>
    

    .htaccess 中不允许使用 &lt;Directory&gt; 指令(因此出现 500 错误)。 &lt;Directory&gt; 容器只能在 servervirtualhost 上下文中使用。 .htaccess 文件本身相当于 &lt;Directory&gt; 容器。 (但您为什么要尝试在此处定位 /nas 目录,因为它似乎是您想要禁用目录列表的 /test 子目录?)

    您需要在您禁用目录列表的/test 子目录中创建另一个.htaccess 文件:

    # Disable auto-generated directory listings (mod_autoindex) for this directory only
    Options -Indexes
    

    现在,您在访问 /test 子目录时只会收到 403 Forbidden。

    或者,如果您不想在子目录中创建另一个 .htaccess 文件,则需要使用 RedirectMatch (mod_alias) 或 RewriteRule (mod_rewrite) 来阻止该特定 URL。例如,在根.htaccess 文件中:

    RewriteEngine On
    
    RewriteRule ^test/?$ - [F]
    

    当尝试直接访问/test/ 子目录时,上述内容也会触发 403(就像为该子目录禁用了 Indexes 选项一样)。该子目录中的所有文件(如果有)仍然可以访问。

    【讨论】:

    • 你是怎么做到的?
    【解决方案2】:

    如果您愿意禁用 /test 文件夹的索引选项,您可以使用

    <Directory "/public_html/test/">
        Options -Indexes
    </Directory>
    

    Option -Indexes 将完全删除对 repo 中测试文件夹的访问。

    如果您只想为 /nas 文件夹保留索引选项,以便外人可以访问此文件夹包含,那么您可以使用以下内容

    <Directory "/public_html/nas/">
       Options +Indexes
    </Directory>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-03
      • 2016-02-22
      • 1970-01-01
      • 2012-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-14
      相关资源
      最近更新 更多