htaccess的前端控制器mod-rewrite麻烦控制器、麻烦、htaccess、rewrite

2023-09-02 11:29:17 作者:狗友很狗i

我知道这应该是简单的,但我有一个很难写我的前端控制器的.htaccess规则。

本/主题/文件夹中包含了所有我的CSS / JS /图像等,所以我不希望他们来传递虽然我的控制器

.htaccess文件位于/ myadminarea /文件夹的根目录。

该网站的根目录(下面的'/'没有.htacess文件/ myadminarea')

在前端控制器我看看URL,然后直接包括文件 - 不过,我想是相当宽容与我接受的URL。

如果该URL是因为我想让它传递虽然前端控制器特定的文件

粉色皮革 T5850处理器索尼CR382评测

如果URL是一个目录(尾随正斜杠)我要假设他们正在寻找的index.php文件夹

之内

下面的规则适用于这样的网址

  mydomain.com/myadminarea/mysection/action/
(负载通过前端控制器mydomain.com/myadminarea/mysection/action/index.php~~V)
 

而翻倒在这样的网址 -

  mydomain.com/myadminarea/mysection/action/index.php
 

这包含一个文件名(它不使用前端控制器只是直接加载文件) - 我知道-f排除的文件重写规则,但我想尽组合,我能想到的和!下面至少当网址没有文件名。当我尝试航线每一个请求(除了那些到主题文件夹),我得到了一个服务器配置错误(500)

这是我的原则

 的RewriteCond%{} REQUEST_FILENAME!-f
重写规则!^(主题)/ /myadminarea/index.php [NC,L]
 

编辑:

补充简明前端控制器

  //哪一页实际上是被要求?
$ URI =爆炸(/,$ _ SERVER [REQUEST_URI]);
//失去第一元件和添加的index.php最后一个,如果它是空白
array_shift($ URI);
如果(端($ URI)==''){
    array_pop($ URI);
    $ URI [] ='的index.php';
}

$ page_requested =破灭('/',$ URI);
如果($ page_requested =='myadminarea /的index.php'){
    $ page_requested = FALSE;
}

includePage($ page_requested);

功能includePage($ page_requested){
    如果(page_requested $&安培;&安培; file_exists(BASE_FILE_PATH $ page_requested)及。&安培; is_file(BASE_FILE_PATH $ page_requested)){
        包括:(BASE_FILE_PATH $ page_requested。);
    } 其他 {
        回声$ page_requested;
    }
}
 

解决方案

在我的重写规则的斜线害了我......

 重写规则!^主题/ * myadminarea /的index.php [L,QSA]
 

不是

 重写规则!^主题/ * /myadminarea/index.php [L,QSA]
 

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;
    }
}

解决方案

The leading slash in my rewrite rule was killing me...

RewriteRule !^themes/* myadminarea/index.php [L,QSA]

NOT

RewriteRule !^themes/* /myadminarea/index.php [L,QSA]