许多新手站长应该不知道nofollow属性有什么用吧。无梦简单介绍一下。
nofollow是HTML文档中a标签的属性值,用来告诉搜索引擎不要追踪该链接。
你的网站A中有一个外链B,搜索引擎访问你的网站A的时候会顺带去访问链接B,这样就分散了搜索引擎的注意力,也就是网站权重。
而nofollow属性的作用就是不让搜索引擎访问你的网站的时候分散精力,去访问与你无关的页面。
添加nofollow属性的方法:在a标签上增加rel="nofollow",下面给出两个链接,看后你就明白了。
<a href="http://www.5meng.cc/">无梦博客</a>
<a href="http://www.5meng.cc/" rel="nofollow">无梦博客</a>
不少新手站长建立网站已经有一段时间了,一个一个的手动增加nofollow属性简直丧心病狂。所以无梦今天给出一段代码,用来自动添加nofollow属性。
自动为WordPress网站添加Nofollow属性
本次修改很简单,涉及的文件只有functions.php一个,所以就直接上代码:
/* 自动给页面的站外链接添加nofollow属性和新窗口打开 无梦博客 www.5meng.cc 开始*/ add_filter( 'the_content', 'cn_nf_url_parse'); function cn_nf_url_parse( $content ) { $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>"; if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) { if( !empty($matches) ) { $srcUrl = get_option('siteurl'); for ($i=0; $i < count($matches); $i++) { $tag = $matches[$i][0]; $tag2 = $matches[$i][0]; $url = $matches[$i][0]; $noFollow = ''; $pattern = '/target\s*=\s*"\s*_blank\s*"/'; preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE); if( count($match) < 1 ) $noFollow .= ' target="_blank" '; $pattern = '/rel\s*=\s*"\s*[n|d]ofollow\s*"/'; preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE); if( count($match) < 1 ) $noFollow .= ' rel="nofollow" '; $pos = strpos($url,$srcUrl); if ($pos === false) { $tag = rtrim ($tag,'>'); $tag .= $noFollow.'>'; $content = str_replace($tag2,$tag,$content); } } } } $content = str_replace(']]>', ']]>', $content); return $content; } /* 自动给页面的站外链接添加nofollow属性和新窗口打开 无梦博客 www.5meng.cc 结束*/
添加代码后,会自动给你的网站所有外链添加rel="nofollow" target="_blank"属性。
已添加过的不会重复添加!
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容