【发布时间】:2015-01-26 08:54:01
【问题描述】:
我目前正在为一个游戏团队开发一个网络项目,我正在尝试测试自己并扩展我的一般知识和工具箱。我正在处理的一项功能要求我“清理”所请求文件的 URL。我有这个系统,它工作得很好。现在,为了让我的网站“完美”运行,我需要通过 htaccess 拒绝访问某些文件夹以及其中的所有文件。但是有一个小问题......它需要在用户甚至没有意识到的情况下运行。
我的文件结构、首选访问方式、路径、状态如下:
rogue {allow} {direct} {%domain%/dashboard/} {200}
css {deny} {direct} {%domain%/css/} {403}
js {deny} {direct} {%domain%/js/} {403}
fonts {deny} {direct} {%domain%/fonts/} {403}
files {deny} {direct} {%domain%/files/} {403}
image {deny} {direct} {%domain%/image/} {403}
includes {deny} {direct} {%domain%/includes/} {403}
php {deny} {direct} {%domain%/php/} {403}
index.php {allow} {direct} {%domain%/dashboard/} {200}
html_footer.php {deny} {direct} {%domain%/dashboard/} {404}
html_header.php {deny} {direct} {%domain%/dashboard/} {404}
.htaccess {deny} {direct} {%domain%/dashboard/} {403}
上面的列表实际上是我希望网站如何运行的一个示例...index.php 文件控制请求的“文件”并相应地显示它们。例如,%domain%/get-in-touch/ 将在%domain%/includes/ 目录中显示contact.php 文件。直接调用index.php显示和%domain%/dashboard/一样。
以下代码来自我的.htaccess 文件。
请忽略# NN sn-ps。这些是为了您的利益,以减少我添加代码 sn-ps 的需要。我想它也会对您有所帮助...如果需要,您可以直接引用行号。
DirectoryIndex index.php # 01
Options All +FollowSymlinks -Indexes # 02
# 03
<IfModule mod_rewrite.c> # 04
RewriteEngine On # 05
#RewriteBase /rogue/ # 06
# 07
RewriteCond %{REQUEST_FILENAME} !-d # 08
RewriteCond %{REQUEST_FILENAME} !-f # 09
RewriteRule ^(.*?)$ index.php [QSA,L] # 10
# 11
RewriteCond %{REQUEST_FILENAME} -d # 12
RewriteRule ^(.*?)(js|image|css|includes|php|files)(.*)$ index.php [QSA,L] # 13
</IfModule> # 14
我注释掉了第 06 行,因为系统似乎不需要它,但我保留它以供我自己参考,以防以后可能需要它。当文件或目录不存在时,08 到 10 行控制重定向到index.php,然后响应文件控制其余部分。文件夹css/js/fonts/image/includes/php 都存在,所以行 12 和 13 阻止访问它们并重定向到 index.php 可以找到“目录”数组和相关的可以选择包含文件。该数组的示例包含在帖子的底部。行 12 和 13 可以移动到行 08 之上并且不会有任何变化,但是从文件中删除它们会导致默认 403 禁止错误触发。
删除行 08、09、12 和 13 会通过加载位于在image、files 和fonts 目录中为MIME type text/html,但是,任何其他目录中的文件似乎都不受影响。
12 和 13 行按要求执行,直到您请求访问这些目录中的文件。文件显示出来,我所有的秘密都暴露在世人面前。结果,向用户显示了某些非常重要的文件。我想阻止任何人访问我选择拒绝访问的任何文件夹中的文件,并将其重定向到index.php 并显示403。我尝试在 12 和 13 行之间添加 RewriteCond %{REQUEST_FILENAME} -f,但页面显示为损坏且无法加载资源。
注意:我不得不从 02 行中删除 -Indexes,因为来自 JavaScript 文件的文件请求将被拒绝,这样做也阻止了 12 和 13 不按预期行事,无论其位置如何。
目录数组 [PHP]
$_INCDIRECTORY = 'includes/';
$_PATHINCLUDES = array(
'dashboard' => array(
'allow' => true,
'path' => $_INCDIRECTORY.'dashboard.php',
// The following must remain FALSE; This value will be changed 'on-the-fly';
'exists' => false,
),
'news' => array(
'allow' => true,
'path' => $_INCDIRECTORY.'news.php',
// The following must remain FALSE; This value will be changed 'on-the-fly';
'exists' => false,
),
'search' => array(
'allow' => true,
'path' => $_INCDIRECTORY.'search.php',
// The following must remain FALSE; This value will be changed 'on-the-fly';
'exists' => false,
),
// Excluded/Denied directories;
'css' => array(
'allow' => false,
'path' => 'css/',
// The following must remain FALSE; This value will be changed 'on-the-fly';
'exists' => false,
),
// Error pages;
'403' => array(
'allow' => true,
'path' => $_INCDIRECTORY.'errors/403.php',
// The following must remain FALSE; This value will be changed 'on-the-fly';
'exists' => false,
),
'404' => array(
'allow' => true,
'path' => $_INCDIRECTORY.'errors/404.php',
// The following must remain FALSE; This value will be changed 'on-the-fly';
'exists' => false,
),
);
【问题讨论】:
标签: php apache .htaccess mod-rewrite redirect