1,后台增加url规则,增加后.导航上,或分页号上,会自动替换为静态的样式.类似www.abc.com/news/2/ 2表示页码
phpcms v9 的后台扩展,url规则,添加两个规则,
一个是名称为category的规则,规则的前面的斜线可以去掉,不过可能影响分页问题
/{$catdir}/|/{$catdir}/{$page}/
url示例为 www.abc.com/news/
一个是名称为show规则
{$catdir}/{$id}.html|{$catdir}/{$id}_{$page}.html
示例为www.abc.com/news/99.html
然后,找到想伪静态的栏目,修改.生成html设置,生成Html全设置为否,url规则选择自己刚才设置的.保存,
最后更新栏目缓存及批量更新url,不更新无效
2,如果为apache的服务器空间,伪静态规则如下,注意,要保存在.htaccess 文件中,并上传到网站根目录中,其它规则自己转换
RewriteEngine on
#静态文件以及API目录不需要伪静态
RewriteRule ^(statics|api|uploadfile)(.*) - [L]
#栏目页
RewriteRule ^([0-9A-Za-z_-]*)/$ index.php?m=content&c=index&a=lists&catdir=$1
RewriteRule ^([0-9A-Za-z_-]*)/([0-9]+)/$ index.php?m=content&c=index&a=lists&catdir=$1&page=$2
#内容页
RewriteRule ^([0-9A-Za-z_-]*)/([0-9]+)\.html$ index.php?m=content&c=index&a=show&catdir=$1&id=$2
#标签列表
RewriteRule ^tag-(.*)-([0-9]+).html index.php?m=content&c=tag&a=lists&tag=$1&page=$2
#上面栏目分页,是完全的字母加数字的形式,如www.abc.com/news/2 ,后面不带其它字符,或加一个/字符,如果服务器伪静态匹配到了"域名/字母/数字/"的组合,则会自动跳转到index.php?m=content&c=index&a=lists&catdir=$1&page=$2这个页面中.所以.前面的规则不可重复,否则会错乱.
3,文件,phpcms\phpcms\modules\content\index.php中
搜索
$catid = intval($_GET['catid']);
一共两处,修改为
if(isset ($_GET['catid'])){
$catid = intval($_GET['catid']);
}else{
$catdir=$_GET['catdir'];
$s=$this->_getCategoryId($catdir);
$catid=$s[0][catid];
}
然后,在最下面
} 这个 大括号的前面增加一个函数,如下
protected 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;
}
4, 打开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;
5.改成这样后,可能会出现一个问题,生成栏目伪静态显示/%7B$catdir%7D/的错误。
解决办法如下:
我们找到/phpcms/modules/admin/classes/cache_api.class.php找到下边代码
public function category() //这个方法里边的下边这个方法
if(!preg_match('/^(http|https):\/\//', $r['url'])) {
$r['url'] = siteurl($r['siteid']).$r['url'];
} elseif ($r['ishtml']) {
$r['isdomain'] = '1';
}
$categorys[$r['catid']] = $r;
修改成下边
if(!preg_match('/^(http|https):\/\//', $r['url'])) {
$r['url'] = preg_replace('/(\{\$catdir\})/i',$r['catdir'],siteurl($r['siteid']).$r['url']);
} elseif ($r['ishtml']) {
$r['isdomain'] = '1';
}
$r['url'] = preg_replace('/(\{\$catdir\})/i',$r['catdir'],$r['url']);
$categorys[$r['catid']] = $r;
然后找到/phpcms/modules/admin/category.php里边的public function cache()
里边有跟上边相同的代码修改了就可以了!
phpcms v9自定义栏目伪静态造成页面无法404问题
phpcms v9自定义栏目伪静态,网上有很多的教程,也可以参考我的文章:PHPCMS V9自定义栏目伪静态实现方法(列表页/分页/内容页),其基本原理就是通过伪静态规则匹配到对应的栏目目录,然后通过目录搜寻对应的栏目catid,也正是因为这样,就造成了无论匹配到什么样的目录,都会直接去list或者show方法下去寻找对应的栏目和内容,但是这两个方法中,如果传递了空的catid或者id之后只会提示:文章不存在或者您没有访问该信息的权限! 这不是我们想要看到的结果,所以需要加个判断
修改方法:打开phpcms\modules\content\index.php
(1)找到66行左右的代码
$r = $this->db->get_one(array('id'=>$id));
在这个后边加上代码:
if(!$r){
header('location:/404.html');
}
(2)找到220行左右的(此时是在自定义伪静态基础上的修改)
if(isset ($_GET['catid'])){
$catid = intval($_GET['catid']);
}else{
$catid=$this->_getCategoryId($_GET['dir']);
}
在这后边加上代码:
if(empty($catid)){
header('location:/404.html');
}
此时就可以实现当栏目或者内容不存在的时候跳转到404页面,当然别忘了在根目录下添加404.html