为什么这个.htaccess文件阻止用户访问我想要保护的内容?

时间:2012-03-16 01:00:03

标签: apache .htaccess mod-rewrite url-rewriting

我正在尝试用这个.htacess文件完成一些事情,但似乎无法做到 得到它来提供我需要的重写,同时防止对我想要的文件进行无条件访问 隐。我的目标是允许通过/ section_name /访问位于/ sections / section_name / webroot /中的任何文件。因此,如果存在,/ admin / images / kittens / cat.jpg将提供/sections/admin/webroot/images/kittens/cat.jpg。我希望能够有多个部分。如果未指定某个部分,但该文件存在于/ sections / default / webroot中,那么我希望能够提供该部分。任何其他要求应该 转到/dispatcher.php。我以为我有这个工作,直到我请求配置 文件在/ config并且能够看到它。然后我意识到我基本上可以查看任何文件 如果我知道那条路。

如何在保持重写工作的同时解决此安全问题?

这是我的.htacess文件:

Options +FollowSymlinks -MultiViews -Indexes
RewriteEngine On

# If a file is requested in the admin webroot, and it exists, allow it to pass through
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^admin(/.*)$ sections/admin/webroot/$1 [L,QSA]

# if the requested url begins with /customers and it is located in /sections/customers/webroot
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^customers(/.*)$ sections/customers/webroot/$1 [L,QSA]

# if the requested url begins with /resellers and it is located in /sections/resellers/webroot
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^resellers(/.*)$ sections/resellers/webroot/$1 [L,QSA]

# if the requested file does not begin with /admin, /customers, or /resellers, and is in /sections/default/webroot, then serve it
RewriteCond %{DOCUMENT_ROOT}/sections/default/webroot/$1 -f
RewriteRule ^(.*)$ sections/default/webroot/$1 [QSA,L,NC]

# Send everything else to the dispatcher
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ dispatcher.php [QSA,L]

2 个答案:

答案 0 :(得分:0)

我知道这不是一个很好的解决方案,但是你是否尝试过将它一直切断然后逐一添加规则,一路上测试每个规则?首先,如果你可以在/ config中获取你不应该的文件,我会尝试删除除最终规则之外的所有内容(它应该在/ config中向dispatcher发送请求)并查看是否有效。如果是这样,请继续慢慢添加内容,直到您看到允许/配置文件的特定规则。然后你知道要解决什么。

答案 1 :(得分:0)

将此添加到最终工作。任何不是其中一个请求的有效文件的东西 webroot文件夹,并不是对dispatcher.php的调用,被重定向到调度程序。

RewriteCond %{REQUEST_URI} !=/dispatcher.php
RewriteCond %{REQUEST_URI} !^/sections/[a-zA-Z0-9_-]+/webroot/
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.*)$ dispatcher.php [QSA,L]