【问题标题】:Apache - Deny access to all directories except publicApache - 拒绝访问除公共目录外的所有目录
【发布时间】:2013-08-13 12:45:42
【问题描述】:

我正在开发一个工具,但我被困在这一点上:我想为每个目录定义一组规则,基本上我只想要“公共”文件夹可用,并拒绝访问其他文件夹。

我的目录结构是

uapi(root)/
    Application
    Config
    Public/
         Index.php
    Storage
    .htaccess

这里是 .htaccess 文件

<Directory /Public>
    Order Deny, Allow
    Allow from all
    <IfModule mod_rewrite.c>
        Options +FollowSymLinks
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ Index.php/$1 [L]
    </IfModule>
</Directory>

<Directory /Config>
    Order Deny, Allow
    Deny from all
</Directory>

<Directory /Application>
    Order Deny, Allow
    Deny from all
</Directory>

<Directory /Storage>
    Order Deny, Allow
    Deny from all
</Directory>

【问题讨论】:

  • 也许可以尝试删除/注释掉 Rewrite... 指令,如果可行的话,问题就在那里。
  • 我查看了 access.log 和它的 kepps 说
  • 一般来说,如果您可以控制服务器并且不在共享主机上,那么将这些东西放在服务器配置(httpd.conf)中会更好/更快,因为 apache 不需要寻找和在每个请求上解析 .htaccess 文件。如果你是共享主机,你会被 .htaccess 方法困住(在大多数情况下)
  • 为什么不在根目录中的单个 .htaccess 文件“拒绝所有”和 /Public 文件夹中的 .htaccess 文件与“全部允许” /Public 文件夹中的 .htaccess 文件应该覆盖.htaccess 文件在根目录?这样可以避免在每个文件夹 /Application、/Config 等中都有一个 .htaccess 文件。

标签: apache .htaccess


【解决方案1】:

正如doc page 所说,您不能在 htaccess 中使用 &lt;Directory&gt; 指令,而只能在服务器 conf 文件中使用。

无论如何,这对您来说不是问题:您可以在每个目录中存储一个.htaccess 文件,例如。创建这些文件:

公共/.htaccess

Order Deny, Allow
Allow from all
<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ Index.php/$1 [L]
</IfModule>

/Config/.htaccess

Order Deny, Allow
Deny from all

/Application/.htaccess

Order Deny, Allow
Deny from all

/Storage/.htaccess

Order Deny, Allow
Deny from all

【讨论】:

【解决方案2】:

您可以在主 DOCUMENT_ROOT/.htaccess 文件中使用 mod_rewrite 本身来完成所有这些操作。使用此代码:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^(Application|Storage)(/.*|)$ - [NC,F]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^Public/(.*)$ /Public/Index.php/$1 [L,NC]

此代码将对/Storage/*/Application/* 的所有请求抛出Forbidden 错误,但会让/Public/*/Public/index.php 处理。

【讨论】:

    【解决方案3】:

    对于 Apache httpd server 2.4,您可以这样做:

    在 httpd.conf 或 httpd/conf.d/ 目录中的 .conf 文件中。

    <Directory "/uapi/">
      Require all denied
    </Directory>
    
    <Directory "/uapi/public">
      Require all granted
    </Directory>
    

    我测试了这个配置。参考是here

    如果您无权访问 Apache 配置文件,您也应该可以在 .htaccess 文件中执行此操作。 那就是,在 /uapi/.htaccess 中:

    Require all denied
    

    在 /uapi/Public/.htaccess 中

    Require all granted
    

    如上所述,您不能在 .htaccess 文件中使用。来自Apache docs

    Note that unlike &lt;Directory&gt; and &lt;Location&gt; sections, &lt;Files&gt; sections can be used inside .htaccess files.

    更多通用文档: https://httpd.apache.org/docs/2.4/howto/access.html

    【讨论】:

      猜你喜欢
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-11
      • 2017-08-05
      相关资源
      最近更新 更多