A-A+
wordpress实现外链跳转的方法
今天确实上班确实比较累,也不打算更新项目相关的内容了,今天分享一个郑智仁博客自用的外链跳转教程,这个教程应该是相当有用吧,对于保护网站权重很有用,防止权重流失也很有效。同时这篇文章也是为了以后如果有其它改动,我还借鉴以备不时之需。
以下方法来源于张戈博客:分享一个WordPress外链跳转教程,兼容知更鸟暗箱下载和文章索引 | 张戈博客
①、新增跳转
根据以前分享的方法,在网站根目录新增一个文件夹,命名为go,并在go文件夹下新增一个index.php,内容如下:PHP
1 2 3 4 | <?php //$t_url=$_GET['url']; //此代码无法支持带请求参数的目的地址,已弃用! $t_url = preg_replace('/^url=(.*)$/i','$1',$_SERVER["QUERY_STRING"]); //这个支持 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://zhangge.net/'; $title='参数错误,正在返回首页...'; } } } else { $title='参数缺失,正在返回首页...'; $url='http://zhangge.net/'; } ?> <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> |
现在用浏览器访问http://域名/go/?url=外链就能实现跳转了, 比如访问: http://zzr6.com/go/?url=http://www.baidu.com 就能跳转到百度了。
②、新增robots规则:
为了防止搜索引擎抓取这种跳转链接,我们可以在robots.txt里面新增禁止抓取/go的规则:PHP
1 2 3 4 5 | ...以上内容略... Disallow: /go ...以下内容略... |
③、重写外链
i. 替换文章内容中的外链
在主题目录下的functions.php新增如下函数,即可将文章中的外链替换为go跳转的形式:PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //给外部链接加上跳转 add_filter('the_content','the_content_nofollow',999); function the_content_nofollow($content) { preg_match_all('/<a(.*?)href="(.*?)"(.*?)>/',$content,$matches); if($matches){ foreach($matches[2] as $val){ if(strpos($val,'://')!==false && strpos($val,home_url())===false && !preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i',$val)){ $content=str_replace("href=\"$val\"", "href=\"".home_url()."/go/?url=$val\" ",$content); } } } return $content; } |
ii. 替换评论者的链接
在主题目录下的functions.php查找是否存在修改评论链接为新窗口commentauthor函数,如果存在则如下修改第8行,将$url修改为/go/?url=$url,其实就是在前面新增一个go跳转即可,相同的道理!PHP
1 2 3 4 5 6 7 8 9 | //评论链接新窗口 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>"; } |
Ps:如果functions里面没有这个评论新窗口的函数,请自己找到评论列表输出的代码位置(可能在comments.php),然后参考修改即可(国内主题一般都会有个评论新窗口函数,自己仔细找找看)!
Thanks for sharing your thoughts about 建站. Regards