【问题标题】:How do I use .htaccess to lock one directory but password protect another?如何使用 .htaccess 锁定一个目录但密码保护另一个目录?
【发布时间】:2018-10-10 06:11:36
【问题描述】:

例如,假设我有 5 个目录。 5 个中的 3 个我不想通过浏览器建立索引,第 4 个我只想通过密码访问,第 5 个我不需要被锁定并且可以通过浏览器访问。如何使用 httd.conf 文件实现此目的?

我知道在需要密码保护的目录中需要 .htaccess/.htpasswd 文件,我需要从 Options 指令中删除 Indexing 以使这 3 个目录无法访问。

但我不知道如何在其他两个设置到位的情况下公开查看 1 个目录。有人请帮忙!谢谢。

【问题讨论】:

  • 有什么问题? .htaccess 应用到他们所在的目录。
  • 我想通过浏览器从除 2 之外的所有目录中删除索引。在 2 中,我需要在一个上输入密码,而另一个我想对所有人开放。我如何实现这一目标? 1. 从 options 指令中删除索引, 2. 将 .htaccess & .htpasswd 放在我想要保护的目录中,但是如何实现#3(让一个目录对所有人开放?

标签: .htaccess


【解决方案1】:

这对我来说看起来很简单,但是由于您没有提供太多细节,因此我必须做出一些假设,并且可能是错误的。

  • 我假设您的所有目录都直接在您的DocumentRoot 下,并且没有嵌套在另一个目录中。
  • 您拥有默认配置,主服务器中的 AllowOverrideOptions 指令或 VirtualHost 配置没有任何更改。
  • Apache 2.4 版

另外,你提到了主要的httpd.conf.htaccess,所以我只给你主要的httpd.conf 文件的配置。

# Don't AutoIndex dir1, dir2 or dir3
<DirectoryMatch "/path/document/root/dir(1|2|3)$">
    Options -Indexes
</DirectoryMatch>

# Protect dir4
<Directory "/path/document/root/dir4">
    AuthType basic
    AuthName "private"
    # Adjust for the location of your htpasswd. DONT put it inside your DocumentRoot
    AuthUserFile /etc/httpd/example.htpasswd
    Require valid-user
</Directory>

# This directive isn't necessary at all provided you didn't change defaults
<Directory "/path/document/root/dir5">
    Options +Indexes
    Require all granted
</Directory>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 2013-10-20
    • 1970-01-01
    • 2012-08-12
    • 2013-10-18
    • 2014-02-07
    相关资源
    最近更新 更多