修改重写规则以附加查询字符串并继续


Mod rewrite rule to append query string and continue

不确定是否有合理的方法来做到这一点,或者我是否遗漏了什么。我已经构建了一个内容管理系统,它通过模板系统(本质上是一个完成处理的PHP文件)传递所有内容。考虑到这一点,以下规则将向lucy.php发送请求,在那里验证url并加载适当的模板。

RewriteRule ^$ lucy.php [L,QSA]
RewriteRule ^([^/'.]+)/?$ lucy.php?section1=$1 [L,QSA]
RewriteRule ^([^/'.]+)/([^/'.]+)/?$ lucy.php?section1=$1&section2=$2 [L,QSA]
RewriteRule ^([^/'.]+)/([^/'.]+)/([^/'.]+)/?$ lucy.php?section1=$1&section2=$2&section3=$3 [L,QSA]

因此,URL的每一部分都作为一个节#变量发送到脚本。我的问题是,当我使用一个需要自己的URL系统的模板时。在这种情况下。。。博客。通常,我会做一些类似的事情

RewriteRule ^blog/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^/'.]+)/?$ site/blog.php?year=$1&month=$2&day=$3&blog_url=$4 [L,QSA]

将请求发送到一次性脚本。我现在的做法是http://domain.com/blog仍将执行前面提到的lucy.php脚本,其中加载并显示博客模板。那么,是否可以继续允许请求的/blog部分路由到适当的重写规则,但也可以将年、月、日和blog_url字段附加到查询字符串中?/blog url将不一致,甚至可能不会被称为博客,所以我需要一些动态工作的东西。

我唯一的想法是复制lucy.php重写的每个实例,以包括基于日期的结构的可选参数。类似。。。

RewriteRule ^([^/'.]+)/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^/'.]+)/?$ lucy.php?section1=$1&year=$2&month=$3&day=$4&item_url=$5 [L,QSA]

但我认为可能有更有效的方法。该约定的另一个问题是,我希望它适用于其他场景,如类别、作者和其他非博客场景。我不想为这些实例中的每一个都重复lucy.php重写块。想法?

传递完整的URL,然后用PHP进行解析,而不是编写新规则

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /lucy.php?url=$1 [QSA,L]

编辑:

在PHP中,我使用这个。。。

    function parseUrl(){
    $tempArray  = explode('/', $_GET['url']);
    foreach($tempArray as $section){
        if(stristr($section,':')){
            $tempParamArray = explode(':',$section);
            if(stristr($tempParamArray[1],',')){
                $tempParamArray[1] = explode(',',$tempParamArray[1]);
            }
            $this->parameters[$tempParamArray[0]] = $tempParamArray[1];
        }else{
            if(!empty($section)){                   
                array_push($this->sections,$section);
            }
        }
    }
}

然后我使用URLS,比如。。。

/第1节/第2节/第3节/

/博客/日期:2013-01-01/