立即註冊  找回密码

QQ登录

只需一步,快速开始

开启辅助访问 切换到宽版

東里論壇

搜索
查看: 11141|回复: 0

[使用心得] Discuz! X3.3优化修改(一)——有利于SEO的多种伪静态

[复制链接]

335

主题

486

帖子

3089

积分

管理員

Rank: 9Rank: 9Rank: 9

积分
3089
发表于 2017-1-26 16:54:31 |阅读模式
Discuz! X在后台是可以设置伪静态的,但实际上有很多地方应该采用伪静态的地方,依旧是动态链接,比如论坛首页的“最后发表”(最新回复)帖子链接,帖子上的“复制链接”(包括主楼和各楼层都是动态),“上一主题/下一主题”链接,“电梯直达”(楼层跳转)链接,等等等等,太多了。这些地方,一直以来就都没有做伪静态,官方的Discuz! X3.3也只是增加对PHP7的支持,以及做了一些补丁工作,所以我们只能自己动手了。

以下修改,只在Discuz! X3.3验证通过,其他版本不保证效果。

首先,我们要去掉一个PC端的多余参数——我认为是多余的。进入后台—全局—SEO设置—URL静态化,里面有个:论坛主题内容页,程序伪静态设置默认是:thread-{tid}-{page}-{prevpage}.html,其中“{tid}”是帖子ID,“{page}”是帖子分页页数,“{prevpage}”是主题列表页(帖子所在版块分页)页数。比如说,你某一帖子位于某一版块的第3页,“{prevpage}”就是“3”,如果有人回复,帖子被顶到第1页来,那“{prevpage}”就会变成“1”。所以在thread-{tid}-{page}-{prevpage}.html中,前两二数值是固定的,而第三个是变化的。既然我们都伪静态了,你一个页面地址变来变去的怎么个意思?这样变来变去,是违反链接唯一性的要求的。其实这个参数没啥用,只是在你点击“返回列表”的时候起作用,但却会造成一个页面多个链接的问题,这对SEO是不利的!而且我这么多年,只习惯用浏览器的“后退”键,我都没注意到这个“返回列表”。所以,直接去掉他吧!注意去掉后,要重新生成Rewrite规则,制作.htaccess。

如果是老论坛,已经有很多链接被收录,则可以增加一条Rewrite,将thread-{tid}-{page}-{prevpage}.html301跳转到thread-{tid}-{page}.html就行

第二,论坛首页“最后发表”帖子链接静态化。打开/source/module/forum/ 下的forum_redirect.php,找到:
  1.         dheader('Location: forum.php?mod=viewthread&tid='.$_G['tid'].$pageadd.'#lastpost');
复制代码
改为:
  1. header("HTTP/1.1 301 Moved Permanently");
  2. dheader('Location:thread-'.$_G['tid'].'-'.$page.'.html#lastpost');
复制代码


第三、帖子(主楼)“复制链接”伪静态实现方法。打开 /template/default/forum/viewthread.htm,将:
  1. forum.php?mod=viewthread&tid=$_G[tid]$fromuid
复制代码
改为:
  1. forum.php?mod=viewthread&tid=$_G[tid]
复制代码


第四、楼层号上复制楼层地址的链接伪静态化。打开 /template/default/forum/viewthread_node.htm模板文件,查找:
  1. <a href="{if $post[first]}forum.php?mod=viewthread&tid=$_G[tid]$fromuid{else}forum.php?mod=redirect&goto=findpost&ptid=$_G[tid]&pid=$post[pid]$fromuid{/if}"  {if $fromuid}title="{lang share_url_copy_comment}"{/if} id="postnum$post[pid]" onclick="setCopy(this.href, '{lang post_copied}');return false;">
复制代码
改为:
  1. <a href="{if $post[first]}forum.php?mod=viewthread&tid=$_G[tid]{else}thread-$_G[tid]-$page.html#pid$post[pid]{/if}" title="点此复制本楼层链接" id="postnum$post[pid]" onclick="setCopy(this.href, '{lang post_copied}');return false;">
复制代码


第五、“只看该作者”伪静态化。打开 /template/default/forum/viewthread_node.htm,找到:
  1.                                                 <span class="pipe">|</span>
  2.                                                 <!--{if !IS_ROBOT && !$_GET['authorid'] && !$_G['forum_thread']['archiveid']}-->
  3.                                                         <a href="forum.php?mod=viewthread&tid=$post[tid]&page=$page&authorid=$post[authorid]" rel="nofollow">{lang thread_show_author}</a>
  4.                                                 <!--{elseif !$_G['forum_thread']['archiveid']}-->
  5.                                                         <a href="forum.php?mod=viewthread&tid=$post[tid]&page=$page" rel="nofollow">{lang thread_show_all}</a>
  6.                                                 <!--{/if}-->
复制代码
改为:
  1. <!--{if !IS_ROBOT && !$_GET['authorid'] && !$_G['forum_thread']['archiveid']}-->
  2. <span class="pipe">|</span><a href="thread-$post[tid]-1-u{$post[authorid]}.html" rel="nofollow">{lang thread_show_author}</a>
  3. <!--{elseif !IS_ROBOT && !$_G['forum_thread']['archiveid']}-->
  4. <span class="pipe">|</span><a href="forum.php?mod=viewthread&tid=$post[tid]&page=$page" rel="nofollow">{lang thread_show_all}</a>
  5. <!--{/if}-->
复制代码

同时在根目录的.htaccess中添加伪静态规则,下面只列出Apache规则:

  1. RewriteCond %{QUERY_STRING} ^(.*)$
  2. RewriteRule ^thread-([0-9]+)-([0-9]+)-u([0-9]+)\.html$ forum.php?mod=viewthread&tid=$1&authorid=$3&page=$2
复制代码

注意以下各条的Apache规则,要放在.htaccess中

  1. RewriteCond %{QUERY_STRING} ^(.*)$
  2. RewriteRule ^([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ plugin.php?id=$1:$2&%1
复制代码
的前面,否则有可能会无效。


第六、“倒序/正序浏览”伪静态化。打开 /template/default/forum/viewthread_node.htm,查找:
  1.                                                 <!--{if $ordertype != 1}-->
  2.                                                         <span class="pipe show">|</span><a href="forum.php?mod=viewthread&tid=$_G[tid]&extra=$_GET[extra]&ordertype=1"  class="show">{lang post_descview}</a>
  3.                                                 <!--{else}-->
  4.                                                         <span class="pipe show">|</span><a href="forum.php?mod=viewthread&tid=$_G[tid]&extra=$_GET[extra]&ordertype=2"  class="show">{lang post_ascview}</a>
  5.                                                 <!--{/if}-->
复制代码
改为:
  1. <!--{if $ordertype != 1}-->
  2. <span class="pipe show">|</span><a href="thread-$_G[tid]-1-o1.html" rel="nofollow" class="show">{lang post_descview}</a>
  3. <!--{else}-->
  4. <span class="pipe show">|</span><a href="thread-$_G[tid]-1-o2.html" rel="nofollow" class="show">{lang post_ascview}</a>
  5. <!--{/if}-->
复制代码

同样在.htaccess中添加Apache伪静态规则:

  1. RewriteCond %{QUERY_STRING} ^(.*)$
  2. RewriteRule ^thread-([0-9]+)-([0-9]+)-o([0-9]+)\.html$ forum.php?mod=viewthread&tid=$1&ordertype=$3&page=$2
复制代码


第七、“只看大图”伪静态。打开/template/default/ forum/viewthread_node.htm 模板文件,查找
  1. <a href="forum.php?mod=viewthread&tid=$_G[tid]&from=album">{lang view_bigpic}</a>
复制代码
,改为:
  1. <a href="thread-$_G[tid]-album.html" rel="nofollow"><font color="#336699">相册模式</font></a>
复制代码

同样在.htaccess中添加Apache伪静态规则:
  1. RewriteCond %{QUERY_STRING} ^(.*)$
  2. RewriteRule ^thread-([0-9]+)-album\.html$ forum.php?mod=viewthread&tid=$1&from=album
复制代码


第八、“打印”帖子伪静态。打开 /template/default/forum/viewthread.htm 模板文件,查找:
  1. href="forum.php?mod=viewthread&action=printable&tid=$_G[tid]"
复制代码
改为:
  1. href="printable-$_G[tid].html" rel="nofollow"
复制代码

同样在.htaccess中添加Apache伪静态规则:

  1. RewriteCond %{QUERY_STRING} ^(.*)$
  2. RewriteRule ^printable-([0-9]+)\.html$ forum.php?mod=viewthread&action=printable&tid=$1
复制代码

再在打印页添加帖子链接,打开/template/default/ forum/viewthread_printable.htm,搜
  1. <b>{lang subject}: </b>$_G[forum_thread][subject] <b><a href="###" onclick="this.style.visibility='hidden';window.print();this.style.visibility='visible'">[{lang thread_print}]</a></b></span><br />
复制代码
在后面添加:
  1. 本帖地址:<a href='{$_G[siteurl]}thread-$_G[tid]-1.html' title='$_G[forum_thread][subject]'>{$_G[siteurl]}thread-$_G[tid]-1.html</a><br /><br />
复制代码
保存时注意编码。

这一条设置为 rel="nofollow",是不追踪本链接的意思,robots.txt 中的Disallow: /printable-*.html是禁止搜索引擎收录,由于我们在模板中增加了回到帖子的链接,如果你想取消这两个设置也不是不行。

第九、“上一主题/下一主题”链接伪静态化。打开/source/module/forum/forum_redirect.php,搜:
  1. dheader("Location: forum.php?mod=viewthread&tid=$next");
复制代码
改为:
  1. dheader("Location: thread-$next-1.html");
复制代码


第十、“电梯直达”伪静态。打开source/module/forum/forum_redirect.php,搜:

  1.         dheader("Location: forum.php?mod=viewthread&tid=$tid&page=$page$authoridurl$ordertypeurl".(isset($_GET['modthreadkey']) && ($modthreadkey = modauthkey($tid)) ? "&modthreadkey=$modthreadkey": '')."#pid$pid");
复制代码
改为:
  1.         dheader("Location: thread-$tid-$page.html$authoridurl$ordertypeurl".(isset($_GET['modthreadkey']) && ($modthreadkey = modauthkey($tid)) ? "&modthreadkey=$modthreadkey": '')."#pid$pid");
复制代码
再搜:
  1.                 if($ptid) {
  2.                         header("HTTP/1.1 301 Moved Permanently");
  3.                         dheader("Location: forum.php?mod=viewthread&tid=$ptid");
复制代码
改为:
  1.                 if($ptid) {
  2.                         header("HTTP/1.1 301 Moved Permanently");
  3.                         dheader("Location: thread-$tid-1.html");
复制代码


最后,在根目录 robots.txt 文件中添加:
  1. Disallow: /printable-*.html
  2. Disallow: /thread-*f*.html
  3. Disallow: /thread-*o*.html
  4. Disallow: /thread-*u*.html
  5. Disallow: /thread-*album.html
复制代码


以上效果,大家可以在“花木成蹊论坛”体验一下:http://www.huamuchengxi.com/forum.php
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即註冊

本版积分规则

本版推荐主题

QQ|网站地图|小黑屋|Archiver|東里論壇 ( 粤ICP备14015598号 ) 公安备案图标粤公网安备 44512202000019号
GMT+8, 2024-4-24 13:01 , Processed in 0.090867 second(s), 57 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.