我知道这应该很简单,但我很难为我的前端控制器编写 .htaccess 规则。
/themes/ 文件夹包含我所有的 css/js/images 等,所以我不希望它们通过我的控制器
.htaccess 文件位于 /myadminarea/ 文件夹的根目录中。
站点的根目录(在 '/' 上的 '/myadminarea' 下面没有 .htacess 文件)
在前端控制器中,我查看 URL,然后直接包含文件 - 但是我想对我接受的 Urls 非常宽容..
如果 url 是针对特定文件的,我希望它通过前端控制器
如果 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 排除了文件的重写规则,但我已经尝试了我能想到的所有组合以及下面的最少适用于没有文件名的 url。当我尝试路由每个请求(除了那些到主题文件夹的请求)时,我收到服务器配置错误(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;
}
}