phpcms v9自定义栏目伪静态实现方法(列表页/分页/内容页)

一、编写.htaccess中的urlrewrite规则

首先看urlrewrite的规则,这个是Apache下的,其它环境下的规则自己转换下

RewriteEngine on
#静态文件以及API目录不需要伪静态
RewriteRule ^(statics|api|uploadfile)(.*) - [L]
#内容页
RewriteRule ^([0-9A-Za-z_/]*)/([0-9]+).html index.php?m=content&c=index&a=show&dir=$1&id=$2
RewriteRule ^([0-9A-Za-z_/]*)/([0-9]+)_([0-9]+).html index.php?m=content&c=index&a=show&dir=$1&id=$2&page=$3
#栏目页
RewriteRule ^([0-9A-Za-z_/]*)/index_([1-9][0-9]*).html index.php?m=content&c=index&a=lists&dir=$1&page=$2
RewriteRule ^([0-9A-Za-z_/]*)/ index.php?m=content&c=index&a=lists&dir=$1


二、更新phpcms/modules/content目录下的index.php


1、在 //首页  前面新增如下函数

private function _getCategoryId($catdir){
       if(!strpos($catdir,'/')) {
         $dirname = $catdir;
        }else {
         $dirname = end(explode('/',$catdir));
                }
       $this->category_db = pc_base::load_model('category_model');
       $result = $this->category_db->get_one(array('catdir'=>$dirname));
       return $result['catid'];
}

2、分别在`lists()`和`show()`方法中,将获取`catid`的语句修改

$catid = intval($_GET['catid']

替换

$catid = intval($_GET['catid']);
  if(isset ($_GET['catid'])){
      $catid = intval($_GET['catid']);
    }else{
      $catid=$this->_getCategoryId($_GET['dir']);
}

3、找到 //URL规则下面的

$GLOBALS['URL_ARRAY']['categorydir'] = $categorydir;
$GLOBALS['URL_ARRAY']['catdir'] = $catdir;
$GLOBALS['URL_ARRAY']['catid'] = $catid;

替换

$GLOBALS['URL_ARRAY']['categorydir'] = $parentdir;
$GLOBALS['URL_ARRAY']['catdir'] = $catdir;
$GLOBALS['URL_ARRAY']['catid'] = $catid;


三、然后在phpcms后台,添加两条自定义的URL规则

URL规则栏目页:  {$categorydir}{$catdir}/|{$categorydir}{$catdir}/index_{$page}.html

URL规则内容页:    {$categorydir}{$catdir}/{$id}.html|{$categorydir}{$catdir}/{$id}_{$page}.html

然后再更新栏目缓存、批量更新URL就可以看到效果了


四、修改phpcms/modules/content/classes目录中的url.class.php 找到if (!$setting['ishtml']) { //如果不生成静态将下面的:

$url = str_replace(array('{$catid}', '{$page}'), array($catid, $page), $urlrule);   
  if (strpos($urls, '')!==false) {  
  $url = APP_PATH.str_replace('', '/', $urls);   
}

替换成:

$domain_dir = '';
if (strpos($category['url'], '://')!==false && strpos($category['url'], '?')===false) {
    if (preg_match('/^((http|https)://)?([^/]+)/i', $category['url'], $matches)) {
        $match_url = $matches[0];
        $url = $match_url.'/';
    }

    $db = pc_base::load_model('category_model');
    $r = $db->get_one(array('url'=>$url), '`catid`');
    if($r) $domain_dir = $this->get_categorydir($r['catid']).$this->categorys[$r['catid']]['catdir'].'/';
}

$categorydir = $this->get_categorydir($catid);
$catdir = $category['catdir'];
$year = date('Y',$time);
$month = date('m',$time);
$day = date('d',$time);
//echo $catdir;
$urls = str_replace(array('{$categorydir}','{$catdir}','{$year}','{$month}','{$day}','{$catid}','{$id}','{$prefix}','{$page}'),array($categorydir,$catdir,$year,$month,$day,$catid,$id,$prefix,$page),$urlrule);
// echo $urls."<br>";
if (strpos($urls, '')!==false) {
$urls = APP_PATH.str_replace('', '/', $urls);
}   
$url = $domain_dir.$urls;


更新栏目缓存就OK了。

上一篇:最后一页

下一篇:PHPCMS v9 tag标签调用不了缩略图