【发布时间】:2011-04-18 15:30:17
【问题描述】:
我知道这应该很简单,但我很难为我的前端控制器编写 .htaccess 规则。
/themes/ 文件夹包含我所有的 css/js/images 等,所以我不希望它们通过我的控制器
.htaccess 文件位于 /myadminarea/ 文件夹的根目录中。
站点的根目录(在“/”上的“/myadminarea”下面没有.htacess 文件)
在前端控制器中,我查看 URL,然后直接包含文件 - 但是我希望对我接受的 Urls 非常宽容..
如果网址是针对特定文件的,我希望它通过前端控制器
如果 url 用于目录(尾部正斜杠),我想假设他们正在该文件夹中寻找 index.php
以下规则适用于这样的网址
mydomain.com/myadminarea/mysection/action/
(loads mydomain.com/myadminarea/mysection/action/index.php via the front controller)
但在这样的网址上掉了 -
mydomain.com/myadminarea/mysection/action/index.php
包含文件名(它不使用前端控制器,而只是直接加载文件)-我知道 !-f 排除了文件的重写规则,但我已经尝试了我能想到的所有组合以下至少适用于没有文件名的网址。当我尝试路由每个请求(除了那些到主题文件夹的请求)时,我收到服务器配置错误 (500)
这是我的规则
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(themes)/ /myadminarea/index.php [NC,L]
编辑:
添加了精简的前端控制器
// what page is actually being requested?
$uri = explode("/", $_SERVER["REQUEST_URI"]);
//lose the first element and add index.php the last one if it is blank
array_shift($uri);
if (end($uri) == ''){
array_pop($uri);
$uri[] = 'index.php';
}
$page_requested = implode('/', $uri);
if ($page_requested == 'myadminarea/index.php'){
$page_requested = false;
}
includePage($page_requested);
function includePage($page_requested){
if ($page_requested && file_exists(BASE_FILE_PATH . $page_requested) && is_file(BASE_FILE_PATH . $page_requested)){
include(BASE_FILE_PATH . $page_requested);
} else {
echo $page_requested;
}
}
【问题讨论】:
-
为了记录,主题周围的()是不必要的。
-
你的前端控制器是如何工作的?它的文件名是什么?
-
@M'vy 我已经添加了我的前端控制器
-
前端控制器是index.php文件吗?
标签: .htaccess mod-rewrite front-controller