- A+
网站的文章里有时可能会引用一些别人网站的链接,如果这样的链接过多,必然会导致网站权重的流失,所以下面介绍几种解决方案。
第一种:
为这样的链接添加rel="external nofollow" 属性 ,意思是告诉搜索引擎的蜘蛛这个链接非本站链接,不要爬行也不要传递权重。这种方案可以通过JavaScript批量为这样的外部链接加上rel="external nofollow"属性,否则单个的加很麻烦。
第二种:
新建一个go.php文件,放置到wordpress的根目录下,在go.php里面输入:
<?php header("location:".$_SERVER['QUERY_STRING']); ?>
然后保存后,可用的外链跳转形式为: {本站地址}/go.php?url={外链地址},再添加外链的时候,只要给外链加上统一的跳转前缀:http://网站地址/go.php?url= 即可。
第二种方法:
同样,新建一个go.php文件,放置到wordpress的根目录下,在go.php里面输入:
<?php header("location:".$_SERVER['QUERY_STRING']); ?>
然后保存,可用的外链跳转形式为: {本站地址}/go.php?{外链地址} 在添加外链的时候,只要给外链加上统一的跳转前缀:http://网站地址/go.php? 即可。
以上方法均需要在手动添加外链的时候,加上跳转前缀,比如 http://网站地址/go.php?外链地址,并不是很方便,所以得考虑写到functions.php里面去。(以下代码均来自张戈博客)。
第一步:新建一个文件放入以下代码,保存为go.php,将go.php文件上传网站根目录。
<?php $t_url = $_GET['url']; if (!empty($t_url)) { preg_match('/(http|https):\/\//', $t_url, $matches); if ($matches) { $url = $t_url; $title = '页面加载中,请稍候...'; } else { preg_match('/\./i', $t_url, $matche); if ($matche) { $url = 'http://' . $t_url; $title = '页面加载中,请稍候...'; } else { $url = 'http://www.zhoubo99.com/'; $title = '参数错误,正在返回首页...'; } } } else { $title = '参数缺失,正在返回首页...'; $url = 'http://www.zhoubo99.com/'; } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="refresh" content="1;url='<?php echo $url;?>';"> <title> <?php echo $title;?> </title> <style> body{background:#000;}.loading{-webkit-animation:fadein 2s;-moz-animation:fadein 2s;-o-animation:fadein 2s;animation:fadein 2s}@-moz-keyframes fadein{from{opacity:0}to{opacity:1}}@-webkit-keyframes fadein{from{opacity:0}to{opacity:1}}@-o-keyframes fadein{from{opacity:0}to{opacity:1}}@keyframes fadein{from{opacity:0}to{opacity:1}}.spinner-wrapper{position:absolute;top:0;left:0;z-index:300;height:100%;min-width:100%;min-height:100%;background:rgba(255,255,255,0.93)}.spinner-text{position:absolute;top:50%;left:50%;margin-left:-90px;margin-top: 2px;color:#BBB;letter-spacing:1px;font-weight:700;font-size:36px;font-family:Arial}.spinner{position:absolute;top:50%;left:50%;display:block;margin-left:-160px;width:1px;height:1px;border:25px solid rgba(100,100,100,0.2);-webkit-border-radius:50px;-moz-border-radius:50px;border-radius:50px;border-left-color:transparent;border-right-color:transparent;-webkit-animation:spin 1.5s infinite;-moz-animation:spin 1.5s infinite;animation:spin 1.5s infinite}@-webkit-keyframes spin{0%,100%{-webkit-transform:rotate(0deg) scale(1)}50%{-webkit-transform:rotate(720deg) scale(0.6)}}@-moz-keyframes spin{0%,100%{-moz-transform:rotate(0deg) scale(1)}50%{-moz-transform:rotate(720deg) scale(0.6)}}@-o-keyframes spin{0%,100%{-o-transform:rotate(0deg) scale(1)}50%{-o-transform:rotate(720deg) scale(0.6)}}@keyframes spin{0%,100%{transform:rotate(0deg) scale(1)}50%{transform:rotate(720deg) scale(0.6)}} </style> </head> <body> <div class="loading"> <div class="spinner-wrapper"> <span class="spinner-text"> 页面加载中,请稍候... </span> <span class="spinner"> </span> </div> </div> </body> </html>
也可以保存为index.php文件放入根目录下的go文件夹。
同时需要在robots.txt文件中屏蔽这这个跳转文件
在robots.txt文件中添加 Disallow: /go 或者 Disallow: go.php
上面这一步只是一个跳转函数,并没有实现网站里的外链进行跳转的功能。
第二步:重写外链:
- 替换文章内容中的外链
在主题目录下的functions.php新增如下函数,即可将文章中的外链替换为go跳转的形式:
2. 替换评论者的链接
在主题目录下的functions.php查找是否存在修改评论链接为新窗口commentauthor函数,如果存在则如下修改第8行,将$url修改为/go/?url=$url,其实就是在前面新增一个go跳转即可,相同的道理!
如果functions里面没有这个评论新窗口的函数,请自己找到评论列表输出的代码位置(可能在comments.php),然后参考修改即可(国内主题一般都会有个评论新窗口函数,自己仔细找找看)!
//评论者链接跳转并新窗口打开 function commentauthor($comment_ID = 0) { $url = get_comment_author_url( $comment_ID ); $author = get_comment_author( $comment_ID ); if ( empty( $url ) || 'http://' == $url ) echo $author; else echo "<a href='".home_url()."/go/?url=$url' rel='external nofollow' target='_blank' class='url'>$author</a>"; }
3、评论内容中链接重定向
//评论内容中链接重定向 add_filter('get_comment_author_link', 'add_redirect_comment_link', 5); add_filter('comment_text', 'add_redirect_comment_link', 99); function add_redirect_comment_link($text = ''){ $text=str_replace('href="', 'href="'.get_option('home').'/go/?url=', $text); return $text; }
4. 针对本主题的弹出层下载链接重定向
在主题下的functions.php文件中写入:
function link_nofollow($url) { if(strpos($url,'://')!==false && strpos($url,home_url())===false) { $url = str_replace($url, home_url()."/go/?url=".$url,$url); } return $url; }
修改主题下inc/file.php文件:
原代码:
<?php if (get_post_meta($post->ID, 'button1', true)): ?> <div id="button_box"> <div id="button_file"> <h3>文件下载</h3> <div class="file_ad" align="center"><?php echo stripslashes(zm_get_option('ad_f')); ?></div> <div class="buttons"> <?php if (get_post_meta($post->ID, 'button1', true)): ?> <?php $button1 = get_post_meta($post->ID, 'button1', true); ?> <?php $url1 = get_post_meta($post->ID, 'url1', true); ?> <a href="<?php echo $url1; ?>" rel="external nofollow" target="_blank"><?php echo $button1; ?></a> <?php endif; ?> <?php if (get_post_meta($post->ID, 'button2', true)): ?> <?php $button2 = get_post_meta($post->ID, 'button2', true); ?> <?php $url2 = get_post_meta($post->ID, 'url2', true); ?> <a href="<?php echo $url2; ?>" rel="external nofollow" target="_blank"><?php echo $button2; ?></a> <?php endif; ?> <?php if (get_post_meta($post->ID, 'button3', true)): ?> <?php $button3 = get_post_meta($post->ID, 'button3', true); ?> <?php $url3 = get_post_meta($post->ID, 'url3', true); ?> <a href="<?php echo $url3; ?>" rel="external nofollow" target="_blank"><?php echo $button3; ?></a> <?php endif; ?> <?php if (get_post_meta($post->ID, 'button4', true)): ?> <?php $button4 = get_post_meta($post->ID, 'button4', true); ?> <?php $url4 = get_post_meta($post->ID, 'url4', true); ?> <a href="<?php echo $url4; ?>" rel="external nofollow" target="_blank"><?php echo $button4; ?></a> <?php endif; ?> </div> <div class="clear"></div> </div> </div> <?php endif; ?>
修改后的代码:
<?php if (get_post_meta($post->ID, 'button1', true)): ?> <div id="button_box"> <div id="button_file"> <h3>文件下载</h3> <div class="file_ad" align="center"><?php echo stripslashes(zm_get_option('ad_f')); ?></div> <div class="buttons"> <?php if (get_post_meta($post->ID, 'button1', true)): ?> <?php $button1 = get_post_meta($post->ID, 'button1', true); ?> <!-- 用我们自定义的函数过滤一下即可 --> <?php $url1 = link_nofollow(get_post_meta($post->ID, 'url1', true)); ?> <a href="<?php echo $url1; ?>" rel="external nofollow" target="_blank"><?php echo $button1; ?></a> <?php endif; ?> <?php if (get_post_meta($post->ID, 'button2', true)): ?> <?php $button2 = get_post_meta($post->ID, 'button2', true); ?> <?php $url2 = link_nofollow(get_post_meta($post->ID, 'url2', true)); ?> <a href="<?php echo $url2; ?>" rel="external nofollow" target="_blank"><?php echo $button2; ?></a> <?php endif; ?> <?php if (get_post_meta($post->ID, 'button3', true)): ?> <?php $button3 = get_post_meta($post->ID, 'button3', true); ?> <?php $url3 = link_nofollow(get_post_meta($post->ID, 'url3', true)); ?> <a href="<?php echo $url3; ?>" rel="external nofollow" target="_blank"><?php echo $button3; ?></a> <?php endif; ?> <?php if (get_post_meta($post->ID, 'button4', true)): ?> <?php $button4 = get_post_meta($post->ID, 'button4', true); ?> <?php $url4 = link_nofollow(get_post_meta($post->ID, 'url4', true)); ?> <a href="<?php echo $url4; ?>" rel="external nofollow" target="_blank"><?php echo $button4; ?></a> <?php endif; ?> </div> <div class="clear"></div> </div> </div> <?php endif; ?>
