«

EMLOG文章外链自动本地化或直接外链加nofollow属性的方法

时间:2015-8-2 15:20     作者:admin     分类: emlog


做网站的都或多或少动那么一点SEO
知识,在发文章的时候对于站外链接都想加上一个nofollow的属性,以防止本站的权重不流失,手动给每个链接增加nofollow有些费事,于是乎就
想到了用PHP代码自动把非本站网址的链接加上nofollow属性,今天分享的这个PHP代码就是这个用处,你只需要把下面代码复制粘贴到指定位置后,
你就不用担心每次都手动给文章内容里的站外链接添加nofollow属性了;链接添加nofollow属性不易过多,适当才好,这个还是站长自己把握吧,
有需要的就从下方把代码复制去吧。

    将下面代码复制到module.php文件里去


<pre style="overflow:auto" ;class="prettyprint lang-php linenums"><?php
function content_nofollow($log_content, $domain){
preg_match_all('/href="(.?)"/', $log_content, $matches);
if ($matches) {
foreach ($matches[1] as $val) {
if (strpos($val, $domain) === false) {
$log_content = str_replace('href="'.$val.'"', 'href="'.$val.'" rel="external nofollow" ', $log_content);
}}}
preg_match_all('/src="(.
?)"/', $log_content, $matches);
if ($matches) {
foreach ($matches[1] as $val) {
if (strpos($val, $domain) === false) {
$log_content = str_replace('src="'.$val.'"', 'src="'.$val.'" rel="external nofollow" ', $log_content);
}}}
return $log_content;
}?>
<pre style="overflow:auto" ;class="prettyprint lang-php linenums">将<?php echo content_nofollow($log_content,BLOG_URL);?>下面代码复制到echo_log.php、page.php里替换<?php echo $log_content; ?>
也可以把外链本地化(ps:如果不懂,请直接使用上面的方法即可,本方法为本人自己使用),第一步:请参考《非插件实现Emlog评论链接本地化的方法》中推荐使用放方法

第二步:把上面的代码稍稍修改一下,具体如下



<pre style="overflow:auto" ;class="prettyprint lang-php linenums"><?php
function content_nofollow($log_content, $domain){
preg_match_all('/href="(.?)"/', $log_content, $matches);
if ($matches) {
foreach ($matches[1] as $val) {
if (strpos($val, $domain) === false) {
$log_content = str_replace('href="'.$val.'"', 'href="'.BLOG_URL.'go/?url='.$val.'"', $log_content);
}}}
preg_match_all('/src="(.
?)"/', $log_content, $matches);
if ($matches) {
foreach ($matches[1] as $val) {
if (strpos($val, $domain) === false) {
$log_content = str_replace('src="'.$val.'"', 'src="'.BLOG_URL.'go/?url='.$val.'"', $log_content);
}}}
return $log_content;
}?>


标签: emlog

版权所有:Mrxn's Blog
文章标题:EMLOG文章外链自动本地化或直接外链加nofollow属性的方法
除非注明,文章均为 Mrxn's Blog 原创,请勿用于任何商业用途,转载请注明作者和出处 Mrxn's Blog

扫描二维码,在手机上阅读

推荐阅读:

评论:
avatar
李明 2016-09-16 21:03
不错的方法