在使用 Discuz 构建论坛时,分页和参数过多可能导致搜索引擎识别出大量重复内容,影响 SEO 排名。为了解决这个问题,我们可以在 版块页面(forumdisplay.php) 中添加 canonical 标签,指向标准 URL,提升搜索引擎友好度。
什么是 Canonical 链接?
<link rel=”canonical” href=”URL”> 是一种 HTML 标签,用于告诉搜索引擎“当前页面的标准版本地址”。它能有效避免:
- 分页造成的重复内容;
- 参数变化导致的多个 URL 指向同一内容;
- SEO 权重分散。
操作步骤
1. 找到模板文件
打开 /source/module/forum/forum_forumdisplay.php 文件98行添加以下代码
在下面的代码中插入
$forumseoset = array(
'seotitle' => $_G['forum']['seotitle'],
'seokeywords' => $_G['forum']['keywords'],
'seodescription' => $_G['forum']['seodescription']
);
// Canonical URL逻辑开始
$_GET['page'] = max(1, intval($_G['page']));
if(is_array($_G['setting']['rewritestatus']) && in_array('forum_forumdisplay', $_G['setting']['rewritestatus'])) {
$canonical = rewriteoutput('forum_forumdisplay', 1, '', $_G['fid'], $_G['page'], '', '');
} else {
$canonical = 'forum.php?mod=forumdisplay&fid='.$_G['fid'].($_G['page'] > 1 ? '&page='.$_G['page'] : '');
}
$_G['setting']['seohead'] .= '';
// Canonical URL逻辑结束