DEDEBIZ伪静态URL地址和生成静态URL地址保持一致的方法
DEDEBIZ是由原DEDECMS开发而来的,DedeBIZ系统基于PHP7版本开发,代码进行了优化重构,延续之前的标签,以前的模板拿过来都是可以直接使用的,如果原来用的DEDECMS的系统,执行一下升级程序, 就可以转到DEDEBIZ程序,DEDEBIZ对URL地址进行了修改,如果是生成静态,URL地址是和以前DEDCMS一样的,如果是用伪静态,它的地址全部改写了,即下面格式:
栏目URL地址为:网址/list/栏目ID(如栏目一的URL地址为/list/1,栏目二的URL地址为/list/2,栏目三的URL地址为/list/3)
栏目分页URL地址为:网址/list/栏目ID-页码
内容页URL地址:网址/article/文章ID.html
内容分页URL地址:网址/article/文章ID-页码.html
栏目和内容页的URL地址里都是固定死了,只是后面的ID不同。
而以前的DEDECMS的URL地址为:
列表页 http://www.xxxx.com/news/
列表分页 http://www.xxxxcom/news/list_1_2.html
内容页 http://www.xxxx.com/xinwenzixun/6.html
内容页分页 http://www.xxxx.com/news/6_2.html
如果是以前的老站转过来的话,那么URL地址全变了,以前的收录也就全废了。跟官方反映过这个问题,他们坚持用现在新的URL规则。如果想用跟以前一样的URL,得自己二开或者跟官方定制。
如果你是想做企业站。生成静态URL地址能没啥问题,但如果你要用伪静态,那新闻栏目的URL就为/list/1 ,产品栏目为/list/2,URL里面的list是固定的,通过栏目的URL地址,你不知道它是啥栏目,以前的/xinwen/或者/news/一眼就可以看出啥栏目,后台栏目生成目录的地方你填的啥,你的目录就是啥名,这样的URL也方便SEO。
我这里以DEDEBIZ6.5版本为例进行修改,主要实现列表页、内容页、TAG标签页的伪静态。
准备工作:安装好DEDEBIZ系统,打开后台系统设置,开启伪静态,如果你现在的站全是静态,得把它全部转成动态,一健静态转动态方法 DEDEBIZ动静态切换插件 如果是有数据的站,请提前备份好数据。
伪静态设置成功后,访问URL地址效果如下:
列表页 http://www.123.com/news/
列表分页 http://www.123.com/news/list-1-2.html
内容页 http://www.123.com/xinwenzixun/6.html
内容页分页 http://www.123.com/news/6-2.html
TAG标签页 http://www.123.com/tags/126/
TAG标签分页 http://www.123.com/tags/126/2/
如果你是新站,不在意URL的问题,只是想使用伪静态,后台开启伪静态后,只需要使用下面的伪静态规则即可实现列表,内容页的伪静态
伪静态规则(Nginx)
#内容页及内容页分页
rewrite ^/article/([0-9]+).html$ /apps/view.php?aid=$1;
rewrite ^/article/([0-9]+)-([0-9]+).html$ /apps/view.php?aid=$1&PageNo=$2;
#列表页及列表分页
rewrite ^/list/([0-9]+)$ /apps/list.php?tid=$1;
rewrite ^/list/([0-9]+)-([0-9]+)$ /apps/list.php?tid=$1&PageNo=$2;
#TAG标签
rewrite ^/tags\.html$ /apps/tags.php;
rewrite ^/tags/(.*)(?:(\?.*))* /apps/tags.php?\/$1;
rewrite ^/tags/(.*)\/(?:(\?.*))* /apps/tags.php?\/$1\/;
rewrite ^/tags/(.*)\/([0-9])(?:(\?.*))* /apps/tags.php?\/$1\/$2;
rewrite ^/tags/(.*)\/([0-9])\/(?:(\?.*))* /apps/tags.php?\/$1\/$2\/;
设置好后,它的URL地址效果如下:
列表页 http://www.123.com/list/1
列表分页 http://www.123.com/lsit/1-2.html
内容页 http://www.123.com/article/6.html
内容页分页 http://www.123.com/article/6-2.html
TAG标签页 http://www.123.com/tags/126/
TAG标签分页 http://www.123.com/tags/126/2/
TAG的伪静态开启后,它的URL路径里会包含/app/tags.php?/,可以参考下面的TAG修改方法即可实现上面的URL效果。
废话也不多说了。开始教程
第一步:列表页
1、列表页
打开system\helpers\channelunit.helper.php,大约在145行,
把
$reurl = $GLOBALS['cfg_cmspath']."/list/".$typeid;
修改为:
$reurl = $GLOBALS['cfg_cmspath']."/{$typedir}/";
修改前它的URL地址为/list-1,修改后:/栏目名称
打开/apps/list.php搜索
$tid = (isset($tid) && is_numeric($tid) ? $tid : 0);
大约在13行,把它修改为:
if($cfg_rewrite == 'Y')
{
if(!is_numeric($tid))
{
$typedir = parse_url($tid, PHP_URL_PATH);
$PageNo = stripos(GetCurUrl(), '.html') ? intval(str_replace('.html', '', end(explode("-", GetCurUrl())))) : 1;
$tinfos = $dsql->GetOne("SELECT * FROM `#@__arctype` WHERE typedir='/$typedir' or typedir='{cmspath}/$typedir'");
if(is_array($tinfos))
{
$tid = $tinfos['id'];
$typeid = GetSonIds($tid);
$row = $dsql->GetOne("Select count(id) as total From `#@__archives` where typeid in({$typeid})");
}
else
{
$tid = 0;
}
}
}
else
{
$tid = (isset($tid) && is_numeric($tid) ? $tid : 0);
}
2、列表分页:
打开/archive/listview.class.php,查找://伪静态栏目分页 ,大约在1204行
把
if ($cfg_rewrite == 'Y') {
$plist = str_replace("?tid=", "", $plist);
$plist = preg_replace("/&pageno=(\d+)/i", "-\\1", $plist);
$plist = preg_replace("/&TotalResult=(\d+)/i", "", $plist);//去掉分页数值
}
return $plist;
}
修改为:
if ($cfg_rewrite == 'Y') {
$plist = str_replace("?tid=", "list-", $plist);
$plist = preg_replace("/&pageno=(\d+)/i", "-\\1.html", $plist);
$plist = preg_replace("/&TotalResult=(\d+)/i", "", $plist);//去掉分页数值
}
return $plist;
}
修改前:/list-栏目id-页码
修改后:/栏目/list-栏目id-页码
注意,我这里把URL中间的连接符由下横线_换成-了,如果你想要下横线,请修改上面的-,list.php里面的explode("-",也要修改。不然列表分页报500错误。
第二步:内容页
1、内容页
打开system\helpers\channelunit.helper.php,搜索:
if (!function_exists('GetFileName'))
大约在196行,把196行到241行里的全部替换为:
if (!function_exists('GetFileName')) {
function GetFileName($aid, $typeid, $timetag, $title, $ismake = 0, $rank = 0, $namerule = '', $typedir = '', $money = 0, $filename = '')
{
global $cfg_cmspath, $cfg_arcdir, $cfg_special, $cfg_arc_dirname, $cfg_rewrite;
// 清理路径中的特殊标记和多余斜杠
$cleanPath = function($path) {
return trim(str_replace(['{cmspath}', '//'], ['', '/'], $path), '/');
};
// 获取规范化栏目目录
$getTypedir = function($typeid, $typedir) use ($cleanPath) {
if (!empty($typedir)) {
return $cleanPath($typedir);
}
if ($typeid > 0) {
$row = GetOne("SELECT typedir FROM `#@__arctype` WHERE id='$typeid'");
return $cleanPath($row['typedir'] ?? 'default');
}
return 'default';
};
// 没指定栏目时用固定专题规则
if (empty($namerule)) {
$namerule = $cfg_special.'/{aid}.html';
$typeid = -1;
}
// 动态文档处理
if ($rank != 0 || $ismake == -1 || $typeid == 0 || $money > 0) {
if ($cfg_rewrite == 'Y') {
$typedir = $getTypedir($typeid, $typedir);
return "/{$typedir}/{$aid}.html"; // 格式:/栏目名/文章ID.html
} else {
return $GLOBALS['cfg_phpurl']."/view.php?aid=$aid";
}
}
// 静态文档处理
else {
$articleDir = $cleanPath(MfTypedir($typedir));
$articleRule = strtolower($namerule);
if ($articleRule == '') {
$articleRule = strtolower($GLOBALS['cfg_df_namerule']);
}
if ($typedir == '') {
$articleDir = $cleanPath($GLOBALS['cfg_cmspath'].$GLOBALS['cfg_arcdir']);
}
$dtime = GetDateMk($timetag);
list($y, $m, $d) = explode('-', $dtime);
$arr_rpsource = array('{typedir}', '{y}', '{m}', '{d}', '{timestamp}', '{aid}', '{cc}');
$arr_rpvalues = array(
$articleDir,
$y, $m, $d,
$timetag,
$aid,
dd2char($m.$d.$aid.$y)
);
if ($filename != '') {
$articleRule = dirname($articleRule).'/'.$filename.$GLOBALS['cfg_df_ext'];
}
$articleRule = str_replace($arr_rpsource, $arr_rpvalues, $articleRule);
if (preg_match("/\{p/", $articleRule)) {
$articleRule = str_replace('{pinyin}', GetPinyin($title).'-'.$aid, $articleRule);
$articleRule = str_replace('{py}', GetPinyin($title, 1).'-'.$aid, $articleRule);
}
$articleUrl = '/'.preg_replace("/^\//", '', $articleRule);
if (preg_match("/index\.html/", $articleUrl) && $cfg_arc_dirname == 'Y') {
$articleUrl = str_replace('index.html', '', $articleUrl);
}
return $articleUrl;
}
}
}
打开\apps\view.php
搜索:$t1 = ExecTime(); 大约在13行,在它下面添加
// 修改URL重写部分
if($cfg_rewrite == 'Y')
{
if(!is_numeric($aid))
{
$currentUrl = GetCurUrl();
if(stripos($currentUrl, '.html') !== false) {
// 解析URL格式:/栏目名/文章ID-页码.html
$urlParts = explode('/', trim($currentUrl, '/'));
$lastPart = str_replace('.html', '', end($urlParts));
// 分离文章ID和页码
$aidParts = explode('-', $lastPart);
$aid = isset($aidParts[0]) ? intval($aidParts[0]) : 0;
$pageno = isset($aidParts[1]) ? intval($aidParts[1]) : 1;
if($aid > 0) {
$_GET['pageno'] = $pageno;
$GLOBALS['pageno'] = $pageno;
}
}
}
if($aid > 0) {
$arcrow = GetOneArchive($aid);
if(is_array($arcrow)) {
// 验证URL时忽略页码部分
$baseUrl = preg_replace('/-\d+\.html$/', '.html', GetCurUrl());
$typedir = str_replace('{cmspath}/', '', $arcrow['typedir']);
$typedir = trim($typedir, '/');
$expectedUrl = '/'.$typedir.'/'.$aid.'.html';
if ($baseUrl != $expectedUrl) {
header("HTTP/1.0 404 Not Found");
die("Request Error: URL不匹配");
}
}
}
}
修改后,内容页的URL由/article/文章ID.html,变成/栏目名称/文章ID.html
2、内容分页
打开\system\archive\archives.class.php,搜索: 获得动态文档分页列表 ,大约在919行,把它下面的代码全部替换为:
function GetPagebreakDM($totalPage, $aid)
{
// 0. 单页内容直接返回空字符串
if ($totalPage <= 1) {
return "";
}
// 1. 安全验证(关键修复)
if(empty($aid) || $aid == 1) {
preg_match('/\/(\d+)(?:-|\.)/', $_SERVER['REQUEST_URI'], $matches);
$aid = $matches[1] ?? $this->ArcID ?? 0;
if($aid == 0) return ""; // 改为静默失败
}
// 2. 获取当前页码
$nowPage = 1;
if (preg_match('/-(\d+)\.html$/', $_SERVER['REQUEST_URI'], $matches)) {
$nowPage = (int)$matches[1];
}
// 3. 处理栏目目录
$typedir = $this->TypeLink->TypeInfos['typedir'];
$typedir = str_replace(['{cmspath}/', '{typedir}/'], '', $typedir);
$typedir = trim($typedir, '/');
// 4. 构建分页HTML
$PageList = "<ul class='pagination justify-content-center'>";
$PageList .= "<li class='page-item disabled'><span class='page-link'>共{$totalPage}页</span></li>";
// 上一页(仅当不是第一页时显示)
if ($nowPage > 1) {
$prevUrl = ($nowPage - 1 == 1)
? "/{$typedir}/{$aid}.html"
: "/{$typedir}/{$aid}-".($nowPage - 1).".html";
$PageList .= "<li class='page-item'><a class='page-link' href='{$prevUrl}'>上页</a></li>";
}
// 页码列表
for ($i = 1; $i <= $totalPage; $i++) {
$pageUrl = ($i == 1)
? "/{$typedir}/{$aid}.html"
: "/{$typedir}/{$aid}-{$i}.html";
$PageList .= ($i == $nowPage)
? "<li class='page-item active'><span class='page-link'>{$i}</span></li>"
: "<li class='page-item'><a class='page-link' href='{$pageUrl}'>{$i}</a></li>";
}
// 下一页(仅当不是最后一页时显示)
if ($nowPage < $totalPage) {
$PageList .= "<li class='page-item'><a class='page-link' href='/{$typedir}/{$aid}-".($nowPage + 1).".html'>下页</a></li>";
}
return $PageList;
}
修改后由/article/文章ID-页码.html变成/栏目名称/文章ID-页码.html
第三步:TAG标签
DEDEBIZ的TAG标签已经优化好了。不过它是以ID形式出来的。URL为/tags/209,但它默认的显示是/app/tags.php?/209,我们把它URL地址修改一下就可以了。
打开\system\taglib\tag.lib.php,大约在58行
$row['link'] = $cfg_cmsurl."/apps/tags.php?/".$row['id'];
修改为:
$row['link'] = $cfg_cmsurl."/tags/".$row['id'];
如果你列表页也有调用TAG,请参考下面文章:DEDEBIZ内容页、列表页调用当前文章的TAG标签
里面的TAG标签的URL也需要修改,这里就不多说了。
下面是伪静态规则:
#TAG标签
rewrite ^/tags\.html$ /apps/tags.php;
rewrite ^/tags/(.*)(?:(\?.*))* /apps/tags.php?\/$1;
rewrite ^/tags/(.*)\/(?:(\?.*))* /apps/tags.php?\/$1\/;
rewrite ^/tags/(.*)\/([0-9])(?:(\?.*))* /apps/tags.php?\/$1\/$2;
rewrite ^/tags/(.*)\/([0-9])\/(?:(\?.*))* /apps/tags.php?\/$1\/$2\/;
#列表页
rewrite ^/(.*)/$ /apps/list.php?tid=$1;
#列表分页
rewrite ^/(.*)/list-([0-9]+)-([0-9]+)\.html$ /apps/list.php?tid=$1&PageNo=$2;
#文章页
rewrite ^/(.*)/([0-9]+).html$ /apps/view.php?aid=$2 last;
#文章分页 URL:/栏目名/文章ID-页码.html
rewrite ^/(.*)/([0-9]+)-([0-9]+)\.html$ /apps/view.php?aid=$2&PageNo=$3 last;
注意:设置伪静态后,访问后台时,需要加上index.php,不然会报dedebiz错误。
如果觉得每次加上index.php比较麻烦,可以在列表伪静态规则里加上排除这个目录(?!user|admin)
#列表页
rewrite ^/(?!user|admin)(.*)/$ /apps/list.php?tid=$1;
我这目前添加的是user和admin这两个目录。
END
1.本站主要是为了记录工作、学习中遇到的问题,可能由于本人技术有限,内容难免有纰漏,一切内容仅供参考。
2.本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!
3.本站所有原创作品,包括文字、资料、图片、网页格式,转载时请标注作者与来源。