【问题标题】:.htaccess deny access to all except to one file.htaccess 拒绝访问除一个文件之外的所有文件
【发布时间】:2011-03-25 08:47:57
【问题描述】:

如何拒绝访问整个文件夹和子文件夹,但一个文件除外? 该文件是:toon.php,并且位于同一个文件夹中。

【问题讨论】:

    标签: .htaccess


    【解决方案1】:
    Order Allow,Deny
    <FilesMatch "^toon\.php$">
    Allow from all
    </FilesMatch>
    

    这可能是你能得到的最有效的方法。

    【讨论】:

      【解决方案2】:
      AuthGroupFile /dev/null
      AuthName "Private Area"
      AuthType Basic
      AuthUserFile .htpasswd
      require valid-user
      
      
      <Files "toon.php">
      Allow from all
      Satisfy Any
      
      </Files>
      

      为我工作。不要忘记配置 .htpasswd 文件。

      【讨论】:

      • 我喜欢这个答案。只是想补充一点,您可能在服务器配置中需要AllowOverride AuthConfig Limit
      【解决方案3】:
      Deny From All
      <FilesMatch "^toon\.php$">
      Allow From All
      </FilesMatch>
      

      为我工作...

      【讨论】:

      • @AD7six 是的,有区别。这个答案Deny From AllAllow From All 更清楚。显式优于隐式。对此答案 +1。
      • @Basj 该评论应该是对答案的编辑:)
      【解决方案4】:

      您可能想要使用&lt;Directory&gt;&lt;Files&gt; 指令。您可以在此处查看文档:

      http://httpd.apache.org/docs/1.3/mod/core.html#directory

      http://httpd.apache.org/docs/1.3/mod/core.html#files

      简而言之,你想要这样的东西:

      <Directory /folder/without/access >
           Order Deny,Allow
           Deny from All
      </Directory>
      
      <Files "filename">
           Order Deny,Allow
           Allow from All
      </Files>
      

      【讨论】:

        【解决方案5】:

        如果像我一样,您正在寻找一种方法来对除单个文件之外的整个站点/文件夹进行身份验证,您会发现这种方法效果很好:

        #allows a single uri through the .htaccess password protection
        SetEnvIf Request_URI "/testing_uri$" test_uri
        
        AuthName "Restricted Area"
        AuthType Basic
        AuthUserFile /path/to/your/.htpasswd
        AuthGroupFile /
        Require valid-user
        
        #Allow valid-user
        Deny from all
        Allow from env=test_uri
        Satisfy any
        

        示例来自:this site

        【讨论】:

          【解决方案6】:

          将以下规则添加到根目录下的 htaccess 文件中

          RewriteEngine on
          
          
          RewriteRule !toon\.php$ - [F]
          

          这将拒绝访问除 toon.php 之外的所有文件和文件夹。

          【讨论】:

            【解决方案7】:

            WordPress 管理中有一个相关场景,您可能希望限制对特定 IP 的访问,但保留对插件使用的某些特定文件的访问权限:

            ## /wp-admin/.htaccess ##
            Deny from all
            allow from 88.222.123.88 #Home 
            allow from 88.111.211.77 #Office
            
            <Files "admin-ajax.php">
                 Allow from All
            </Files>
            

            【讨论】:

              【解决方案8】:

              使用 HTTP 密码的专用区域的 WordPress 特定配置:

              AuthName "Private Area"
              AuthType Basic
              AuthUserFile .htpasswd
              require valid-user
              
              <Files "admin-ajax.php">
              Allow from all
              Satisfy Any
              </Files>
              

              【讨论】:

                猜你喜欢
                • 2014-01-08
                • 2013-10-03
                • 2011-12-15
                • 1970-01-01
                • 2015-09-13
                • 2020-01-13
                • 1970-01-01
                • 1970-01-01
                • 2012-10-26
                相关资源
                最近更新 更多