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

phpcmsV9 9.5.8、9.5.9都适用。 栏目伪静态的修改方法(支持自定义目录名),官方程序默认伪静态是不支持自定义栏目名的,所以今天就做了以下修改,让其支持!

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

RewriteEngine on 
#静态文件以及API目录不需要伪静态  www.shangxuebavip.com
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 

1、打开phpcms\modules\content目录下的index.php在最后的}?> 前添加:

/**           *根据栏目名获得ID           * @param $catdir           */   
        function _getCategoryId($catdir){    
            $this->category_db = pc_base::load_model('category_model');    
            $result = $this->category_db->select(array('catdir'=>$catdir));    
           // print_r($result);    
            return $result;    
         } 

 

先增加一个方法,用于将`catdir`转换为`catid`

这是参考站长之家的文章修改而来的,其中获取查询category,我其实一直在纠结是要直接读取cache好还是查询数据库好,读取cache又需要遍历数组,栏目的数量一多的话效率恐怕也没有那么高,增加到 //首页 前面

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

在`show()`方法中,将获取`catid`的语句修改

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

替换

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

在lists()方法中,将获取’catid’的语句修改

$catid = $_GET[‘catid’] = intval($_GET[‘catid’]); 

替换

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

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

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

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

找到 //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; 

2、打开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; 

 

3、分页问题处理 \phpcms\libs\functions\global.func.ph

/** 
 * 返回分页路径 
 * 
 * @param $urlrule 分页规则 
 * @param $page 当前页 
 * @param $array 需要传递的数组,用于增加额外的方法 
 * @return 完整的URL路径 
 */ 
  
function pageurl($urlrule, $page, $array = array()) { 
    if(strpos($urlrule, '~')) { 
        $urlrules = explode('~', $urlrule); 
        $urlrule = $page < 2 ? $urlrules[0] : $urlrules[1]; 
    } 
    $findme = array('{$page}'); 
    $replaceme = array($page); 
    if (is_array($array)) foreach ($array as $k=>$v) { 
        $findme[] = '{$'.$k.'}'; 
        $replaceme[] = $v; 
  
    } 
  
    $url = str_replace($findme, $replaceme, $urlrule); 
    $url = str_replace(array('http://','//','~'), array('~','/','http://'), $url); 
    if (!strstr($url,siteurl(get_siteid()))){ 
                $url = siteurl(get_siteid()) . '/' . $url; 
        } 
    return $url;   
  
} 

 

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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHPCMS V9自定义栏目伪静态实现方法如下: 1. 在 PHPCMS 的后台找到“栏目管理”并进入“修改栏目面,找到“栏目目录”一栏,将其改为英文名称,例如“news”。 2. 进入 PHPCMS 的后台,找到“系统设置”并进入“URL设置”面,选择“伪静态模式”并将“扩展名”设为空,然后在“自定义规则”中添加以下规则: RewriteRule ^news/([0-9]+)/?$ index.php?m=content&c=index&a=lists&catid=$1 [L] RewriteRule ^news/index.html$ index.php?m=content&c=index&a=lists&catid=6 [L] 3. 在服务器上开启 Apache 的 mod_rewrite 模块,可以在 Apache 的配置文件(httpd.conf)中添加以下语句: LoadModule rewrite_module modules/mod_rewrite.so 4. 在 PHPCMS 的根目录下创建一个名为“.htaccess”的文件,并将以下代码复制到该文件中: <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> 5. 保存并上传“栏目目录”中的所有文件和文件夹。 6. 在浏览器中输入网站地址加上“news/”,例如“http://example.com/news/”,即可访问自定义栏目的静态面。 注意事项: 1. 请确保服务器已开启 mod_rewrite 模块,并在 Apache 的配置文件中添加了相应的语句。 2. 在修改“栏目目录”和“自定义规则”时,请确保它们的对应关系正确。 3. 如果您不熟悉 Apache 的配置和 mod_rewrite 模块的使用,请谨慎更改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值