.htaccess 前端控制器 mod 重写问题
我知道这应该很简单,但我很难为我的前端控制器编写 .htaccess 规则。
/themes/ 文件夹包含我所有的 css/js/images 等,所以我不希望它们通过我的控制器
。 .htaccess 文件位于 /myadminarea/ 文件夹的根目录中。
站点的根目录(“/”上的“/myadminarea”下方没有 .htacess 文件)
在前端控制器内,我查看 URL,然后直接包含该文件 - 但是我希望对我接受的 URL 非常宽容。 如果 url 用于特定文件,
我希望它通过前端控制器
如果 url 用于目录(尾随正斜杠),我想假设他们正在该文件夹中查找 index.php
以下规则适用于 url像这样
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;
}
}
I know this should be simple but I'm having a hard time writing the .htaccess rule for my front controller.
The /themes/ folder contains all my css/js/images etc so I don't want them to pass though my controller
The .htaccess file is in the root of the /myadminarea/ folder.
The root of the site (below '/myadminarea' on '/' has NO .htacess file)
Inside the front controller I look at the URL and then include the file directly - however I want to be quite forgiving with the Urls I accept..
If the url is for a specific file I want it to pass though the front controller
If the url is for a directory (trailing forward slash) I want to assume they are looking for index.php within that folder
The below rule works for urls like this
mydomain.com/myadminarea/mysection/action/
(loads mydomain.com/myadminarea/mysection/action/index.php via the front controller)
but falls over on urls like this -
mydomain.com/myadminarea/mysection/action/index.php
that contain a filename (it doesn't use the front controller but just loads the file directly) - I know that the !-f excludes the rewrite rule for files, but I've tried every combination I can think of and the below at least works for urls without filenames. When I try to route EVERY request (except those to the themes folder) I get a server configuration error (500)
This is my rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(themes)/ /myadminarea/index.php [NC,L]
edit:
Added Condensed front controller
// 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的重写规则中的前导斜杠快要了我的命……
不是
The leading slash in my rewrite rule was killing me...
NOT