【问题标题】:.htaccess. deny root, allow specific subfolder. Possible?.htaccess。拒绝根,允许特定的子文件夹。可能的?
【发布时间】:2011-11-30 18:39:17
【问题描述】:

我怎样才能拒绝访问http://sub.mydomain.com/,但允许(完全)http://sub.mydomain.com/test(或http://sub.mydomain.com/test/

http://sub.mydomain.com/test/后面有一个magento后端

【问题讨论】:

    标签: php apache .htaccess magento


    【解决方案1】:

    .htaccess 指令apply to that directory, and all subdirectories thereof,因此您应该在 DocumentRoot 中禁止访问,

    http://sub.mydomain.com/.htaccess:

    Order deny,allow
    Deny from all
    

    并在您希望允许访问的任何特定子目录中覆盖它,

    http://sub.mydomain.com/test/.htaccess:

    Order allow,deny
    Allow from all
    

    【讨论】:

    • 这是一个未经检验的假设,但您只需要在任何应该可通过网络访问的子目录中的第二个 .htaccess
    • 对我不起作用 - 在子目录中添加带有“全部允许”的 .htaccess 仍然需要密码。
    • 但是,除了上述内容之外,添加“满足任何”确实可以解决问题。
    【解决方案2】:

    在根目录下一个.htaccess 怎么样,有以下几行?

    RewriteEngine On
    # check if request is for subdomain
    RewriteCond %{HTTP_HOST} ^sub.mydomain.com$ [NC]
    # check if  'test' isnt part of request
    RewriteCond %{REQUEST_URI} !^/test/?(.*)$ [NC]
    # if subdomain and no 'test' part, redirect to main domain...
    RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R,L]
    

    因此,如果存在“/test/”部分,则不会发生重定向...

    【讨论】:

    • 看起来很有趣。我不得不承认,我对 mod_rewrite 并不熟悉。所以我完全不知道 [NC] 和 [R,L] 代表什么......
    • NC 表示不区分大小写,R = 重定向,L = 最后(在此之后停止从 htaccess 应用规则)
    【解决方案3】:

    尝试在 sub.mydomain.com 中创建 .htaccess 文件来拒绝,在 sub.mydomain.com/test 中创建允许。

    或者您可以从http://sub.mydomain.com/ 重定向到拒绝子目录。

    【讨论】:

      【解决方案4】:

      我知道这是一个非常古老的线程,但我已经对这个场景进行了几天的研究,终于让它工作了,因此我想我会分享我的解决方案以供进一步参考。

      从 Apache 2.4 版开始(我猜),可以使用指令 <RequireAll><RequireAny>。 这可用于允许访问特定的子文件夹。

      我的 .htaccess 解决方案(灵感来自这个网站:https://www.the-art-of-web.com/system/apache-authorization/):

      SetEnvIf REQUEST_URI "^/test/.*" PUBLICACCESS
      # Use for multiple subfolders:
      # SetEnvIf REQUEST_URI "^/(?:test|test2|test3|test4)/.*" PUBLICACCESS
      <RequireAny>
          <RequireAll>
              # Public access
              Require env PUBLICACCESS
              Require all granted
          </RequireAll>
          <RequireAll>
              # Require user and password
              AuthType Basic
              AuthName "Secured"
              AuthUserFile /var/www/example.com/.htpasswd
              Require valid-user
          </RequireAll>
      </RequireAny>
      

      【讨论】:

        【解决方案5】:

        我正在使用由 cpanel 驱动的服务器,它将添加 .htaccess 条目,如下所示:

        #----------------------------------------------------------------cp:ppd
        # Section managed by cPanel: Password Protected Directories     -cp:ppd
        # - Do not edit this section of the htaccess file!              -cp:ppd
        #----------------------------------------------------------------cp:ppd
        AuthType Basic
        AuthName "Protected"
        AuthUserFile "/home/****/.htpasswds/sites/****/passwd"
        Require valid-user
        #----------------------------------------------------------------cp:ppd
        # End section managed by cPanel: Password Protected Directories -cp:ppd
        #----------------------------------------------------------------cp:ppd
        

        我想允许访问特定的子文件夹,解决方案是在该子文件夹中创建一个 .htaccess 文件并将以下内容放入其中:

        Satisfy any
        

        【讨论】:

          猜你喜欢
          • 2013-11-27
          • 2016-03-23
          • 2016-04-18
          • 1970-01-01
          • 2015-01-14
          • 2022-06-28
          • 2012-04-14
          • 2013-08-27
          • 2014-09-03
          相关资源
          最近更新 更多