Web

分享本站外链跳转方法,避免权重流失

michaelliunsky · 9月25日 · 2022年 · · · · · 本文共4607个字 · 预计阅读16分钟1273次已读

最近看到了张戈博客的外链跳转,感觉很不错,于是就在自己的博客上面实现了一下,这里分享一下过程。

前言

你或许看见过类似 http://www.***.com/go.php?http://www.******.com 形式的跳转链接,这样是为了站点的 SEO 能够对各种搜索引擎更友好,术语好像就是叫做外链跳转。更重要的是起到了保护自己域名权重的目的。

中间的过渡,很好地避免了外链的导出,又很好的到达了所需的网站,是一个两全齐美的好东东。

下面我们开始正式部署。

正式部署

①、完整跳转代码

<?php 
if(strlen($_SERVER['REQUEST_URI']) > 384 ||
    strpos($_SERVER['REQUEST_URI'], "eval(") ||
	strpos($_SERVER['REQUEST_URI'], "base64")) {
		@header("HTTP/1.1 414 Request-URI Too Long");
		@header("Status: 414 Request-URI Too Long");
		@header("Connection: Close");
		@exit;
}
//通过 QUERY_STRING 取得完整的传入数据,然后取得 url=之后的所有值,兼容性更好
$t_url = preg_replace('/^url=(.*)$/i','$1',$_SERVER["QUERY_STRING"]);

//数据处理
if(!empty($t_url)) {
    //判断取值是否加密
    if ($t_url == base64_encode(base64_decode($t_url))) {
        $t_url =  base64_decode($t_url);
    }
    //对取值进行网址校验和判断
    preg_match('/^(http|https|thunder|qqdl|ed2k|Flashget|qbrowser):\/\//i',$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://'.$_SERVER['HTTP_HOST'];
	        $title='参数错误,正在返回首页...';
	    }
	}
} else {
    $title = '参数缺失,正在返回首页...';
    $url = 'http://'.$_SERVER['HTTP_HOST'];
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="robots" content="noindex, nofollow" />
<noscript><meta http-equiv="refresh" content="1;url='<?php echo $url;?>';"></noscript>
<script>
function link_jump()
{
    //禁止其他网站使用我们的跳转页面
    var MyHOST = new RegExp("<?php echo $_SERVER['HTTP_HOST']; ?>");
    if (!MyHOST.test(document.referrer)) {
         location.href="http://" + MyHOST;
    }
    location.href="<?php echo $url;?>";
}
//延时 1S 跳转,可自行修改延时时间
setTimeout(link_jump,2000);
//延时 50S 关闭跳转页面,用于文件下载后不会关闭跳转页的问题
setTimeout(function(){window.opener=null;window.close();}, 50000);
</script>
<title><?php echo $title;?></title>
<style type="text/css">
a,abbr,acronym,address,applet,article,aside,audio,b,big,blockquote,body,canvas,caption,center,cite,code,dd,del,details,dfn,div,dl,dt,em,embed,fieldset,figcaption,figure,footer,form,h1,h2,h3,h4,h5,h6,header,hgroup,html,i,iframe,img,ins,kbd,label,legend,li,mark,menu,nav,object,ol,output,p,pre,q,ruby,s,samp,section,small,span,strike,strong,sub,summary,sup,table,tbody,td,tfoot,th,thead,time,tr,tt,u,ul,var,video{margin:0;padding:0;border:0;font-size:100%;font:inherit;vertical-align:baseline}body{background:#235b81}#loader-container{width:188px;height:188px;color:#fff;margin:0 auto;position:absolute;top:50%;left:50%;margin-right:-50%;transform:translate(-50%,-50%);border:5px solid #235b81;border-radius:50%;-webkit-animation:borderScale 1s infinite ease-in-out;animation:borderScale 1s infinite ease-in-out}#loadingText{font-family:"Microsoft YaHei",Helvetica,Arial,Lucida Grande,Tahoma,sans-serif,Raleway,sans-serif;font-size:1.4em;position:absolute;top:50%;left:50%;margin-right:-50%;transform:translate(-50%,-50%)}@-webkit-keyframes borderScale{0%{border:5px solid #fff}50%{border:25px solid #235b81}100%{border:5px solid #fff}}@keyframes borderScale{0%{border:5px solid #fff}50%{border:25px solid #235b81}100%{border:5px solid #fff}}
</style>
</head>
<body>
<div id="loader-container"><p id="loadingText">页面加载中...</p></div>
</body>
</html>

将上述代码保存为index.php,在网站根目录下新建go文件夹,上传到go文件夹里就好。

②、Nginx 伪静态

一般都用的是 “go.php?url=外链地址” 的形式,经常也看到有个别网站对外链跳转做了伪静态,也就是 “/go/外链” 这种形式。这次,也试了下,感觉还不错,所以就整了下。只要在 Nginx 中加入如下规则即可:

rewrite ^/go/([^\?]+)$ /go/?url=$1 last;

上述代码如果不知道放到哪,可以直接放在 location / { 的前面即可。加入伪静态规则,并且重启 Nginx 之后,我们就可以通过 “/go/外链加密串” 的方式进行跳转了。

③、Apache 伪静态

本站用的是Apache 服务器,所以还是补充一下 Aapace 伪静态规则:

RewriteRule ^go/(.*)$ /go/?url=$1 [L]

将上述规则代码添加到 .htaccess 文件的第一行即可。

④、WordPress 替换

A. 文章外链替换

//文章外链跳转伪静态版
add_filter('the_content','link_jump',999);
function link_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('/(ed2k|thunder|Flashget|flashget|qqdl):\/\//i',$val)){
	        $content=str_replace("href=\"$val\"", "href=\"".home_url()."/go/".base64_encode($val)."\" rel=\"nofollow\"",$content);
			}
		}
	}
	return $content;
}

B. 评论外链跳转

//评论者链接重定向
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 {
        if (!preg_match(home_url(),$url)) {
            echo "<a href='".home_url()."/go/".base64_encode($url)."' rel='nofollow' target='_blank' class='url'>$author</a>";
        } else {
            echo "<a href='$url' target='_blank' class='url'>$author</a>";
        }
    }	
}

部署后,刷新前台文章或评论,就能看到效果了。

总结

用张戈的一句话:看了还是不太会的朋友,建议多看、多想、多动手、多搜索、少提问,这是网站折腾学习的不二法门!

展示

点击下方链接跳转到百度,就可以在过程中看到效果了!O(∩_∩)O~~

百度一下,你就知道 (baidu.com)

分享本站外链跳转方法,避免权重流失
跳转

页面用的是本博客的主题色,有需要可以自行修改!

2 条回应
  1. Hugo2022-9-25 · 21:22

    学到了!ヾ(≧∇≦*)ゝ