一对一免费咨询:189-6833-3365


phpcmsV9 9.5.8、9.5.9 、9.6都适用。 自定义栏目伪静态的修改方法(支持自定义目录名自定义),官方程序默认伪静态是不支持自定义栏目名的,以下就是实现自定义栏目名伪静态的方法!

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

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

 

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

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


iis环境 web.config 的规则如下:
 

                <rule name="已导入的规则 7">
                    <match url="^([0-9A-Za-z_/]*)/([0-9]+)\.html" ignoreCase="false" />
                    <action type="Rewrite" url="index.php?m=content&amp;c=index&amp;a=show&amp;dir={R:1}&amp;id={R:2}" appendQueryString="false" />
                </rule>
                <rule name="已导入的规则 8">
                    <match url="^([0-9A-Za-z_/]*)/([0-9]+)_([0-9]+)\.html" ignoreCase="false" />
                    <action type="Rewrite" url="index.php?m=content&amp;amp;c=index&amp;amp;a=show&amp;amp;dir={R:1}&amp;amp;id={R:2}&amp;amp;page={R:3}" appendQueryString="false" />
                </rule>
                <rule name="已导入的规则 9">
                    <match url="^([0-9A-Za-z_/]*)/index_([1-9][0-9]*)\.html" ignoreCase="false" />
                    <action type="Rewrite" url="index.php?m=content&amp;c=index&amp;a=lists&amp;dir={R:1}&amp;page={R:2}" appendQueryString="false" />
                </rule>
                <rule name="已导入的规则 10">
                    <match url="^([0-9A-Za-z_/]*)/(index\.html)?" ignoreCase="false" />
                    <action type="Rewrite" url="index.php?m=content&amp;c=index&amp;a=lists&amp;dir={R:1}" appendQueryString="false" />
                </rule>
 

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

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

  1. private function _getCategoryId($catdir){ 
  2.                 if(!strpos($catdir,'/')) { 
  3.                         $dirname = $catdir
  4.                 }else { 
  5.                         $dirname = end(explode('/',$catdir)); 
  6.                 } 
  7.                 $this->category_db = pc_base::load_model('category_model'); 
  8.                 $result = $this->category_db->get_one(array('catdir'=>$dirname)); 
  9.                 return $result['catid']; 
  10.         } 

 

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

  1. $catid = intval($_GET['catid'

替换

  1. //$catid = intval($_GET['catid']); 
  2.                 if(isset ($_GET['catid'])){ 
  3.                         $catid = intval($_GET['catid']); 
  4.                 }else
  5.                         $catid=$this->_getCategoryId($_GET['dir']); 
  6.                 } 

 

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

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

替换

  1. //$catid = intval($_GET['catid']); 
  2.                 if(isset ($_GET['catid'])){ 
  3.                         $catid = intval($_GET['catid']); 
  4.                 }else
  5.                         $catid=$this->_getCategoryId($_GET['dir']); 
  6.                 } 

 

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

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

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

 

找到 //URL规则下面的

  1. $GLOBALS['URL_ARRAY']['categorydir'] = $categorydir
  2. $GLOBALS['URL_ARRAY']['catdir'] = $catdir
  3. $GLOBALS['URL_ARRAY']['catid'] = $catid

替换

  1. $GLOBALS['URL_ARRAY']['categorydir'] = $parentdir
  2. $GLOBALS['URL_ARRAY']['catdir'] = $catdir
  3. $GLOBALS['URL_ARRAY']['catid'] = $catid

 

2、打开phpcms\modules\content\classes目录中的url.class.php,找到

  1. if (!$setting[‘ishtml’]) { //如果不生成静态 

将下面的:

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

替换成:

  1. $domain_dir = '';    
  2.             if (strpos($category['url'], '://')!==false && strpos($category['url'], '?')===false) {    
  3.                 if (preg_match('/^((http|https):\/\/)?([^\/]+)/i'$category['url'], $matches)) {    
  4.                     $match_url = $matches[0];    
  5.                     $url = $match_url.'/';    
  6.                 }    
  7.                 $db = pc_base::load_model('category_model');    
  8.                 $r = $db->get_one(array('url'=>$url), '`catid`');    
  9.   
  10.                 if($r$domain_dir = $this->get_categorydir($r['catid']).$this->categorys[$r['catid']]['catdir'].'/';    
  11.             }    
  12.             $categorydir = $this->get_categorydir($catid);    
  13.             $catdir = $category['catdir'];    
  14.             $year = date('Y',$time);    
  15.             $month = date('m',$time);    
  16.             $day = date('d',$time);    
  17.             //echo $catdir;    
  18.             $urls = str_replace(array('{$categorydir}','{$catdir}','{$year}','{$month}','{$day}','{$catid}','{$id}','{$prefix}','{$page}'),array($categorydir,$catdir,$year,$month,$day,$catid,$id,$prefix,$page),$urlrule);    
  19.                        // echo $urls."";    
  20.                         if (strpos($urls'\\')!==false) {   
  21.                     $urls = APP_PATH.str_replace('\\', '/', $urls);    
  22.             }    
  23.                         $url = $domain_dir.$urls

 

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

  1. /** 
  2.  * 返回分页路径 
  3.  * 
  4.  * @param $urlrule 分页规则 
  5.  * @param $page 当前页 
  6.  * @param $array 需要传递的数组,用于增加额外的方法 
  7.  * @return 完整的URL路径 
  8.  */ 
  9.   
  10. function pageurl($urlrule$page$array = array()) { 
  11.     if(strpos($urlrule'~')) { 
  12.         $urlrules = explode('~'$urlrule); 
  13.         $urlrule = $page < 2 ? $urlrules[0] : $urlrules[1]; 
  14.     } 
  15.     $findme = array('{$page}'); 
  16.     $replaceme = array($page); 
  17.     if (is_array($array)) foreach ($array as $k=>$v) { 
  18.         $findme[] = '{$'.$k.'}'
  19.         $replaceme[] = $v
  20.   
  21.     } 
  22.   
  23.     $url = str_replace($findme$replaceme$urlrule); 
  24.     $url = str_replace(array('http://','//','~'), array('~','/','http://'), $url); 
  25.     if (!strstr($url,siteurl(get_siteid()))){ 
  26.                 $url = siteurl(get_siteid()) . '/' . $url
  27.         } 
  28.     return $url;   
  29.   

© 2008-2019 浙江东美 ALL RIGHTS RESERVED. XMLBAIDU

免责申明:部分内容来自互联网,若侵犯了您的权益,请告知我们删除!

浙ICP备19019195号-1
找网站建设公司就上东美!
189-6833-3365
tel+86-189-6833-3365