apache - 为什么 .htaccess 不阻止 apache 中的文件夹?

标签 apache .htaccess ubuntu

我试图阻止对 apache 中特定文件夹的直接访问。

我的文件夹结构:
/var/www/html - page.html
- 私有(private)文件夹
-CSS
-jss

我正在尝试使用密码保护私有(private)文件夹。

我完成了以下步骤:

1.在私有(private)文件夹中创建一个 .htacess 文件。

.htacess 文件:

AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user

2.在/etc/apache2 中创建一个带有 htpasswd 的 .htpasswd 文件
  • 现在我更改/etc/apache2/apache2.conf 中的一些规则。

  • 但是当我访问 10.0.0.1/private 时,我可以在没有密码的情况下浏览这个目录。
    <Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
    </Directory>
    
    <Directory /usr/share>
        AllowOverride None
        Require all granted
    </Directory>
    
    <Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride AuthConfig
        AllowOverride All
        Require all granted
    </Directory>
    
    
    <Directory /var/www/>
         Options ExecCGI
         AllowOverride None
         Order allow,deny
         Allow from all
         AddHandler cgi-script .cgi
    </Directory>
    
    <Directory /var/www/>
    Options All
    </Directory>
    
    
    AccessFileName .htaccess
    

    还有什么问题吗?我将写入权限设置为 .htaccess.htpasswd
    sudo chmod 777 /var/www/html/private/.htaccess
    


    sudo chmod 777 /etc/apache2/.htaccess
    

    有什么建议么 ?

    最佳答案

    首先,你不应该使用 .htaccess如果您可以编辑 .conf 文件。

    对于您的问题,当 AllowOverride指令设置为 None , .htaccess 文件被完全忽略。在这种情况下,服务器甚至不会尝试读取文件系统中的 .htaccess 文件。
    您可以尝试将此添加到您的 /etc/apache2/apache2.conf :

    <Directory "/var/www/html/private">
        AuthType Basic
        AuthName "Restricted Content"
        AuthUserFile /etc/apache2/.htpasswd
        Require valid-user
    </Directory> 
    

    关于apache - 为什么 .htaccess 不阻止 apache 中的文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40675578/

    相关文章:

    apache - 如何防止 tomcat7 在 apache 代理设置中重复上下文名称

    javascript - 使用 htaccess 隐藏文件扩展名后链接无法正确打开

    php - 本地 apache 服务器上的奇怪 memcache 行为

    php - Composer 在/var/www/html 中找不到composer.json 文件

    php - 为什么不包含 URL 变量时 htaccess 重定向不起作用?

    ubuntu - 用于 Tomcat8 的 SAML2

    Perl REST 流布局

    python - python-ldap 初始化时 httpd 上的段错误 (11)

    java - 尝试在 OfBiz 中保存任务时收到 FK 违规错误

    php - 删除 php/html 扩展名并从规则中排除一个特定文件