Discuz! 帖子外链自动添加nofollow的方法

[复制链接]
查看: 792|回复: 0

33

主题

33

帖子

103

积分

注册会员

Rank: 2

积分
103
发表于 2019-5-23 21:14:48 | 显示全部楼层 |阅读模式
用过论坛的童鞋们都知道,在帖子内容中经常会出现外部链接,或许演示地址的链接,外链出现的过多会导致网站权重降低,在此优化就需要给外链加上rel="nofollow"属性提高优化效果,因为很多帖子是会员发的,会员并不知道这个,也不可能让管理员后期一个个修改,这里给大家介绍一个方法让系统自动把外链都加上nofollow属性,修改方法如下:
1. 打开目录source/function/function_discuzcode.php文件,查找parseurl函数,对照以下代码进行修改:

[代码]php代码:
  1. function parseurl($url, $text, $scheme) {
  2.                 global $_G;
  3.                 if(!$url && preg_match("/((https?|ftp|gopher|news|telnet|rtsp|mms|callto|bctp|thunder|qqdl|synacast){1}:\/\/|www\.)[^\["']+/i", trim($text), $matches)) {
  4.                         $url = $matches[0];
  5.                         $length = 65;
  6.                         if(strlen($url) > $length) {
  7.                                 $text = substr($url, 0, intval($length * 0.5)).' ... '.substr($url, - intval($length * 0.3));
  8.                         }
  9.                 $url = nofollow($url);
  10.                         return '<a href="'.(substr(strtolower($url), 0, 4) == 'www.' ? 'http://'.$url : $url).'" target="_blank">'.$text.'</a>';
  11.                 } else {
  12.                         $url = substr($url, 1);
  13.                         if(substr(strtolower($url), 0, 4) == 'www.') {
  14.                                 $url = 'http://'.$url;
  15.                         }
  16.                         $url = !$scheme ? $_G['siteurl'].$url : $url;
  17.                         return '<a href="'.nofollow($url).'" target="_blank">'.$text.'</a>';
  18.                 }
  19.         }
复制代码
2. 在parseurl函数后面新增nofollow函数,代码如下:
  1. function nofollow($url = '')
  2.         {
  3.             $temp = array();
  4.          
  5.             if( ! empty($url))
  6.             {
  7.                 $temp = parse_url($url);
  8.          
  9.                 if(isset($temp['host']) && $temp['host'] != $_SERVER['HTTP_HOST'])
  10.                 {
  11.                     $url .= '" rel="nofollow"';
  12.                 }
  13.             }
  14.          
  15.             unset($temp);
  16.             return $url;
  17.         }
复制代码



腾讯云
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

精彩图文



在线客服(工作时间:9:00-22:00)
400-600-6565

内容导航

微信客服

Copyright   ©2015-2019  云服务器社区  Powered by©Discuz!  技术支持:尊托网络     ( 湘ICP备15009499号-1 )