网站建设

分享一个WordPress外链跳转教程,兼容知更鸟暗箱下载和文章索引

Jager · 11月13日 · 2014年 · 10700次已读

张戈博客很久很久之前转载过一篇关于博客外链跳转的方法(相关文章),后来安装了 Anylink 插件也就没有用到。近来清点插件时,我将 Anylnk 给淘汰了,换成了网上找到的给外链添加 nofollow 的代码。

一、原版代码

//给外部链接加上跳转,将此代码添加到 wordpress 主题目录的 functions.php 里面即可
add_filter('the_content','the_content_nofollow',999);
function the_content_nofollow($content){
    preg_match_all('/href="(.*?)"/',$content,$matches);
    if($matches){
	    foreach($matches[1] as $val){
		    if( strpos($val,home_url())===false){
                         $content=str_replace("href=\"$val\"", "href=\"$val\" rel=\"external nofollow\" ",$content);
		}
            }
       }
return $content;
}

代码原理也挺简单,通过匹配文章 content 内容,若发现存在外链,就给这个外链 a 标签加上 rel="nofollow"属性。

这个代码常规博客确实可以用,但有可能导致一些特殊链接失效,比如这个代码很可能会让知更鸟主题的下载按钮无法弹出或者文章索引损坏,应该是替换过程中被破坏掉了。

针对这个问题,我对代码进行了第一个改进:

二、改进代码

//给外部链接加上跳转
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;
}

①、加入了对常规 http://或 https://开头的链接判断,就能有效的绕过知更鸟主题的一些特殊链接,就因为这些特殊链接都不带 http://:

strpos($val,'://')!==false

②、2014-11-26 补充:加入了对 完整 a 标签 判断:

preg_match_all('/<a(.*?)href="(.*?)"(.*?)>/',$content,$matches);
...
foreach($matches[2] as $val){

主要目的是为了绕过高亮代码中的一些外部链接,因为在代码中出现自己博客的跳转形式可能会破坏代码功能,比如博客分享的《百度收录查询和显示代码》,若不过滤的话,其中的百度链接将会被加上跳转,对使用者带来困惑:

分享一个WordPress外链跳转教程,兼容知更鸟暗箱下载和文章索引

③、2015-08-31 补充:加入对外链图片的过滤

有朋友反馈使用后,外链图片也跳转了,导致暗箱跪了。确实会有这个情况,所以在代码中加入了图片的过滤:

!preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i',$val)

偶然翻看博客旧文章,又看到了以前分享的跳转办法,觉得可以和现在的外链处理代码结合一下,或许可以相得益彰,更加完善!

详细部署步骤如下:

三、最终代码

①、新增跳转

根据以前分享的方法,在网站根目录新增一个文件夹,命名为 go,并在 go 文件夹下新增一个 index.php,内容如下:

<?php
    $url = $_GET['url'];
    Header("Location:$url");
?>

现在用浏览器访问 http://域名/go/?url=外链就能实现跳转了, 比如访问: https://zhang.ge/go/?url=http://www.baidu.com 就能跳转到百度了。

②、新增 robots 规则:

为了防止搜索引擎抓取这种跳转链接,我们可以在 robots.txt 里面新增禁止抓取/go 的规则:

...以上内容略...

Disallow: /go

...以下内容略...

③、重写外链

i. 替换文章内容中的外链

在主题目录下的 functions.php 新增如下函数,即可将文章中的外链替换为 go 跳转的形式:

//给外部链接加上跳转
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;
}
分享一个WordPress外链跳转教程,兼容知更鸟暗箱下载和文章索引
ii. 替换评论者的链接

在主题目录下的 functions.php 查找是否存在修改评论链接为新窗口 commentauthor 函数,如果存在则如下修改第 8 行,将$url 修改为/go/?url=$url,其实就是在前面新增一个 go 跳转即可,相同的道理!

//评论链接新窗口
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>";
}

分享一个WordPress外链跳转教程,兼容知更鸟暗箱下载和文章索引

Ps:如果 functions 里面没有这个评论新窗口的函数,请自己找到评论列表输出的代码位置(可能在 comments.php),然后参考修改即可(国内主题一般都会有个评论新窗口函数,自己仔细找找看)!

iii. 知更鸟副标题转载来源链接跳转

其实知更鸟的转载来源链接本身就有 nofollow,不过强迫症嘛,还是继续修改下:

打开知更鸟主题目录下的 includes 文件夹,找到 source.php 文件,如下修改$reprinted 所在行即可:

echo '&#8260; 转载:'.'<a href="'.home_url().'/go/?url='.$reprinted.'" rel="external nofollow" target="_blank">原文链接</a>';

分享一个WordPress外链跳转教程,兼容知更鸟暗箱下载和文章索引

看到这里,相信你应该能轻松领悟方法了吧?就是在外链链接之前加上【http://博客域名/go/?url=】即可!需要修改博客哪个位置的外链,只要找到该位置对应的主题模板,然后参考上述代码修改即可!


2015-07-16 最新补充:

有不少朋友留言要我分享张戈博客目前在用的跳转页面代码,好吧,那就分享一下吧!

go.php 的代码如下:

<?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='https://zhang.ge/';
	        $title='参数错误,正在返回首页...';
	    }
	}
} else {
    $title='参数缺失,正在返回首页...';
    $url='https://zhang.ge/';
}
?>
<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 文件夹(没有 go 文件夹就新建一个),实现 https://zhang.ge/go/?url=https://zhang.ge/ 的跳转形式。

更简单的评论者链接跳转:如果想要让评论者链接也弄成这种跳转形式,只要在 WordPress 主题目录下 functions.php 中插入如下代码即可:

//评论者链接重定向
 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;
 }

记得代码中的“/go/?url=”需要根据实际使用的跳转形式修改即可!


 

2016-02-16 最新补充:张戈博客已分享最新跳转代码,更安全效率==>传送门

 

30 条回应
  1. 叶德华 2014-11-13 · 14:48

    张哥你首页是不是开缓存了?

  2. Alick.Li 2014-11-13 · 16:38

    好东西 你不是说不折腾了吗....

  3. 糯米汇 2014-11-14 · 14:11

    我技术不够!呵呵

  4. 陌小雨 2014-11-15 · 8:33

    蛮有用的哟!,网站汗通过备案,感受下是不是快乐许多。。 :grin:

  5. boke123网址大全 2014-11-15 · 21:03

    我这个站的都是直链,有空要好好研究一下才行。

  6. 欧亚出品 2014-11-20 · 6:19

    我今天用在zblog上试试

  7. avatar
    Jager 2014-11-26 · 12:50

    你博客的跳转页面不错,被我偷走了~稍微改良了下。

    • 红岭 2022-4-18 · 20:21

      卧槽,哪里不错啊,全是H图

  8. 舍力博客 2014-12-9 · 13:50

    我用是用这种办法,不够貌似有个代码可以不用再重新区分http://了的。在go文件夹下面的index.php加入代码
    个人觉得这个要好一点,不用在其他地方重新判断

    • avatar
      Jager 2014-12-9 · 13:57

      很明显你没完全弄清楚文章意思。
      在go下面加代码只是用于跳转参数的判断,而在functions加代码却是用于判断是否需要重写url,添加go/?url跳转。

  9. wwwbby 2015-1-14 · 17:12

    老大我下载了一个页面跳转特效,如何将下面函数引用在特效里,我加去无法实现跳转

    • avatar
      Jager 2015-1-14 · 17:29

      :evil: 乱填网址,拒绝回答任何问题。

      • yo花间游 2015-2-5 · 9:41

        做了域名长短的判断??比方说转不了jd.com 但是jddd.com就可以

        • avatar
          Jager 2015-2-5 · 13:47

          正则匹配问题吧?和本文有关系?

  10. 周杰伦视频网 2015-6-22 · 4:28

    张哥,请问下我的怎么打开外部链接时没有“页面加载中,请稍后”的那个动画,单独打开go/index.phph页面可以看见动画。博客文章里的图片要是用的外部链接是不是也会在前面加上跳转的代码。

  11. 苗条的猪 2015-7-16 · 1:31

    怎么打开外部链接时没有“页面加载中,请稍后”的那个动画?

  12. Loser 2015-8-23 · 0:27

    为什么博主评论者链接跳转无效果?

  13. 博业网 2015-8-24 · 20:48

    我用了这个代码不行啊,显示404错误

  14. dd 2015-10-28 · 16:38

    友情链接的页面会自动添加吗,我想用友情链接做个导航站

  15. 林凡博客 2015-10-29 · 16:00

    今天折腾了下,采用了张兄弟的方法,果断很牛逼的实现了,谢谢啦

  16. 健美网 2015-12-5 · 12:08

    支持、早就想要个了、谢谢

  17. 帅气小琦琦 2016-1-5 · 18:55

    遇到一个问题 就是因为站内有磁力链接 有磁力的格式 也有电驴的格式 这两种格式是没有后缀的 请问如何防止这类链接跳转呢

    • avatar
      Jager 2016-1-5 · 20:29

      磁力和电驴也是有特征的,磁力是 magnet: *** ,电驴是 ek啥的。通过这些特征词排除即可。
      好人做到底,贴一下:

      add_filter('the_content','link_to_jump',999);
      	function link_to_jump($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) && !preg_match('/(magnet|ed2k|thunder|Flashget|flashget|qqdl):\/\//i',$val)){
      			    	$content=str_replace("href=\"$val\"", "href=\"".home_url()."/go/?url=$val\" ",$content);
      				}
      			}
      		}
      		return $content;
      	}
  18. 子歇 2016-3-22 · 15:31

    我竟然看着本教程没有捣腾成功- -

  19. SSS 2016-5-26 · 23:57

    这个给全站都添加了go,怎么排除网址!比如友情链接~求戈神帮助~

  20. 草根 2016-12-5 · 3:24

    请问这个跳转代码适用最新版的繁体wp嘛?

  21. 美丫美搭网 2016-12-23 · 23:14

    这个跳转挺好的,可是自定义自段里的连接没法跳转吧,还有如果是做淘宝客的,链接一般都超级长,这段跳转代码还能再封装一层缩短吗?

    • avatar
      Jager 2016-12-25 · 18:03

      淘宝客这个估计不怎么好用,搜下短代码的看看

  22. 达拉崩7 2017-10-6 · 18:18

    请问:外链跳转会把外链的参数都删掉,这个应该怎么解决呢?