phpcms 自定义栏目伪静态实现方法

phpCMS V9栏目伪静态的修改方法(支持自定义目录名),官方程序默认伪静态是不支持自定义栏目名的,所以今天就做了以下修改,让其支持!

首先看 urlrewrite 的规则,这个是 Apache 下的规则:

RewriteEngine on

#栏目页
RewriteRule ^([0-9A-Za-z_-]*)/$ index.php?m=content&c=index&a=lists&catdir=$1
RewriteRule ^([0-9A-Za-z_-]*)/list_([0-9]+)_([0-9]+).html$ index.php?m=content&c=index&a=lists&catdir=$1&catid=$2&page=$3
#内容页
RewriteRule ^([0-9A-Za-z_-]*)/([0-9]+)/([0-9]+).html$ index.php?m=content&c=index&a=show&catdir=$1&id=$3

#标签列表
RewriteRule ^tag-(.*)-([0-9]+).html index.php?m=content&c=tag&a=lists&tag=$1&page=$2

这个是 IIS 下的规则:

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

1.修改 phpcms\modules\content下的 index.php 文件

找到“public function lists() {”,将以下代码:

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

替换成:

if(isset ($_GET['catid'])){
    $catid = intval($_GET['catid']);
}else{
    $catdir=$_GET['catdir'];
    if($catdir==""){
        $catdir=$_GET['categorydir'];
    }
    $s=$this->_getCategoryId($catdir);
    $catid=$s[0][catid];
}

找到“public function show() {”,将以下代码:

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

替换成:

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

并且在最后的 }?> 前添加:

/**
*根据栏目名获得ID
* @param <type> $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;
}

2.修改 phpcms\modules\content\classes 下的 url.class.php 文件

找到“if (!$setting[‘ishtml’]) {”,将以下代码:

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

替换成:

$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.后台URL规则中添加

url示例:newscenter/news/
url规则:/{$categorydir}{$catdir}/|/{$categorydir}{$catdir}/list_{$catid}_{$page}.html

url示例:news/
url规则:/{$catdir}/|/{$catdir}/list_{$catid}_{$page}.html

url示例:news/20200427/1000.html
url规则:{$catdir}/{$year}{$month}{$day}/{$id}.html

4.其他

更新栏目缓存
批量更新URL

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是宽宽呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值