将文章中的站外链接使用类似于go.php?link=的方式进行跳转,相对于使用nofollow标签,更能保护网站的权重。
修改functions.php文件
将文章中站外链接,自动替换成go.php文件跳转,添加nofollow标签,并使用新窗口打开。
/*文章外链跳转*/
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.php?".base64_encode($val)."\" rel=\"nofollow\" target=\"_blank\" ",$content);
}
}
}
return $content;
}
制作go.php文件
在文章根目录添加go.php,收集了两个比较好的跳转文件。
直接跳转
<?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"]);
//此处可以自定义一些特别的外链,不需要可以删除以下5行
if($t_url=="zhangge" ) {
$t_url="//zhangge.net";
} elseif($t_url=="baidu") {
$t_url="https://www.baidu.com/";
}
//数据处理
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="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, 1000);
//延时50S关闭跳转页面,用于文件下载后不会关闭跳转页的问题
setTimeout(function(){window.opener=null;window.close();}, 50000);
</script>
<title><?php echo $title;?></title>
<style type="text/css">
body{background:#555}.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:45%;left:50%;margin-left:-100px;margin-top:2px;color:#000;letter-spacing:1px;font-size:20px;font-family:Arial}.spinner{position:absolute;top:45%;left:50%;display:block;margin-left:-160px;width:1px;height:1px;border:20px solid rgba(255,0,0,1);-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>
确认按钮是否跳转
<?php
// 请将这里的网址改为自己的(顶级)域名地址
$myDomain = 'domain.com';
// 这里用正则提取 $_SERVER["QUERY_STRING"] 而不是直接 get url
// 是因为如果链接中自身带有 GET 参数则会导致获取不完整
preg_match('/url=(.*)/i', $_SERVER["QUERY_STRING"], $jumpUrl);
// 如果没获取到跳转链接,直接跳回首页
if(!isset($jumpUrl[1])) {
header("location:/");
exit();
}
$jumpUrl = $jumpUrl[1];
// 判断是否包含 http:// 头,如果没有则加上
preg_match('/(http|https):\/\//', $jumpUrl, $matches);
$url = $matches? $jumpUrl: 'http://'. $jumpUrl;
// 判断网址是否完整
preg_match('/[\w-]*\.[\w-]*/i', $url, $matche);
// 是否需要给出跳转提示
$echoTips = false;
if($matche){
// 如果是本站的链接,不展示动画直接跳转
if(isMyDomain($url, $myDomain)) {
header("location:{$url}");
exit(); // 后续操作不再执行
}
$title = '页面加载中,请稍候...';
$fromUrl = isset($_SERVER["HTTP_REFERER"])? $_SERVER["HTTP_REFERER"]: ''; // 获取来源url
// 如果来源和跳转后的地址都不是本站,那么就要给出提示
if(!isMyDomain($fromUrl, $myDomain)) {
$echoTips = true;
}
} else { // 网址参数不完整
$url = '/';
$title = '参数错误,正在返回首页...';
}
/**
* 判断是不是自己的域名
* @param $domain 要进行判断的域名
* @param $my 自己的域名
* @return 对比结果
*/
function isMyDomain($domain, $my) {
preg_match('/([^\?]*)/i', $domain, $match);
if(isset($match[1])) $domain = $match[1];
preg_match('/([\w-]*\.[\w-]*)\/.*/i', $domain.'/', $match);
if(isset($match[1]) && $match[1] == $my) return true;
return false;
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<?php
if($echoTips) {
echo '<title>跳转提示</title>';
} else {
echo '<meta http-equiv="refresh" content="0;url='.$url.'">';
echo '<title>'.$title.'</title>';
}
?>
<style>
body{background:#fff;font-family:Microsoft Yahei;-webkit-animation:fadeIn 1s linear;animation:fadeIn 1s linear}
@-webkit-keyframes fadeIn{from{opacity:0}
to{opacity:1}
}@keyframes fadeIn{from{opacity:0}
to{opacity:1}
}#circle{background-color:rgba(0,0,0,0);border:5px solid rgba(0,183,229,0.9);opacity:.9;border-right:5px solid rgba(0,0,0,0);border-left:5px solid rgba(0,0,0,0);border-radius:50px;box-shadow:0 0 35px #2187e7;width:50px;height:50px;margin:0 auto;position:fixed;left:30px;bottom:30px;-moz-animation:spinPulse 1s infinite ease-in-out;-webkit-animation:spinPulse 1s infinite ease-in-out;-o-animation:spinPulse 1s infinite ease-in-out;-ms-animation:spinPulse 1s infinite ease-in-out}
#circle1{background-color:rgba(0,0,0,0);border:5px solid rgba(0,183,229,0.9);opacity:.9;border-left:5px solid rgba(0,0,0,0);border-right:5px solid rgba(0,0,0,0);border-radius:50px;box-shadow:0 0 15px #2187e7;width:30px;height:30px;margin:0 auto;position:fixed;left:40px;bottom:40px;-moz-animation:spinoffPulse 1s infinite linear;-webkit-animation:spinoffPulse 1s infinite linear;-o-animation:spinoffPulse 1s infinite linear;-ms-animation:spinoffPulse 1s infinite linear}
@-webkit-keyframes spinPulse{0%{-webkit-transform:rotate(160deg);opacity:0;box-shadow:0 0 1px #505050}
50%{-webkit-transform:rotate(145deg);opacity:1}
100%{-webkit-transform:rotate(-320deg);opacity:0}
}@-webkit-keyframes spinoffPulse{0%{-webkit-transform:rotate(0deg)}
100%{-webkit-transform:rotate(360deg)}
}#loading-text{position:fixed;left:110px;bottom:35px;color:#736D6D}
@media screen and (max-width:600px){#circle,#circle1{left:0;right:0;top:0;bottom:0}
#circle{margin:120px auto}
#circle1{margin:130px auto}
#loading-text{display:block;text-align:center;margin-top:220px;position:static;margin-left:10px}
}
.warning{max-width: 500px;margin: 20px auto;}
.wtitle {font-size: 22px;color: #d68300;}
.wurl {overflow: hidden;text-overflow: ellipsis;white-space: nowrap;color: #827777;}
.btn {display: inline-block;line-height: 20px;cursor: pointer;border: 1px solid #A9A6A6;padding: 6px 10px;font-size: 14px;text-decoration: none;}
.btn-green {color: #fff;background-color: #238aca;border: 1px solid #238aca;}
.btn:hover {background-color: #A9A6A6;border: 1px solid #A9A6A6;color: #fff;}
</style>
</head>
<body>
<?php if($echoTips) { ?>
<div class="warning">
<p class="wtitle">您将要访问:</p>
<p class="wurl" title="<?php echo $url;?>"><?php echo $url;?></p>
<p>即将跳转至站外网页,我们无法确认该网页是否安全,它可能包含未知的安全隐患。</p>
<a class="btn btn-green" href="<?php echo $url;?>" rel="nofollow">继续访问</a>
<span class="btn" onclick="closePage()">关闭网页</span>
</div>
<script>
function closePage() {
// 通用窗口关闭
window.opener=null;
window.open('','_self');
window.close();
// 微信浏览器关闭
WeixinJSBridge.call('closeWindow');
}
</script>
<?php } else { ?>
<div id="circle"></div>
<div id="circle1"></div>
<p id="loading-text">页面加载中,请稍候...</p>
<?php } ?>
</body>
</html>