如何實現WordPress博客文章正文外鏈無插件代碼自動轉內鏈教程

我們知道做網站會想著盡量增加內鏈,或外部網站鏈入自己的網頁鏈接,以提高蜘蛛抓取的幾率,防止網站權重的流失,當然作為測評站,經常會分享各個IDC主機商促銷活動及其產品購買鏈接,這個時候我們一方面我們不希望我們文章被其他測評站抄襲,更不能鏈出到過多外部鏈接。

幸運的是我們有一些方法實現文章正文外鏈轉內鏈,比如插件,不過本文國外主機測評分享的是一直代碼方式無插件實現WordPress博客文章正文外鏈自動轉內鏈教程,以防網站權重流失防文章被抄襲。


以國外主機測評DUX主題為例:

1,自動給文章的外部鏈接添加nofollow屬性,將如下代碼加到DUX主題文件夾下function-theme.php文件里:

/*
*自動給文章的外部鏈接添加nofollow屬性
*/ 
add_filter('the_content','web589_the_content_nofollow',999);
function web589_the_content_nofollow($content){
 preg_match_all('/href="(http.*?)"/',$content,$matches);
 if($matches){
 foreach($matches[1] as $val){
 if( strpos($val,home_url())===false ) 
 $content=str_replace("href=\"$val\"", "rel=\"nofollow\" href=\"" . get_bloginfo('wpurl'). "/go?url=" .base64_encode($val). "\"",$content);
 }
 }
 return $content;
}

2,跳go內鏈,在網站根目錄下創建名為”go“的文件夾,在go文件夾下創建index.php文件,并在文件里寫入如下代碼:

<?php 
$url = $_GET['url'];
$a = '';
if( $a==$url ) {
 $b = "http://www.lrracing.com/";
// echo 'true';
} else {
 $b = $url;
 $b = base64_decode($b);
}
//Template Name:鏈接跳轉(有過度)
?>

<html>
<head>
<meta charset=utf-8 />
<meta name="robots" content="nofollow">
<meta http-equiv="refresh" content="0.1;url=<?php echo $b; ?>">
<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:41.5%;left:47%;margin:16px 0 0 35px;color:#BBB;letter-spacing:1px;font-weight:700;font-size:9px;font-family:Arial}.spinner{position:absolute;top:40%;left:45%;display:block;margin:0;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>

將$b = “http://www.lrracing.com/”中網址鏈接替換成自己的即可。

3,以上代碼增加了跳轉美化效果,代碼中已經用base64將源鏈接加密,并且加上了nofollow,但恐怕蜘蛛還是能爬行,在Robots禁止所有蜘蛛爬行/go?url目錄!

Disallow: /go/
Disallow: /go?url

通過以上步驟,就可以實現WordPress博客文章正文外鏈無插件代碼自動轉內鏈了~

贊(0)
版權聲明:本文采用知識共享 署名4.0國際許可協議 [BY-NC-SA] 進行授權, 轉載請注明出處。
文章名稱:《如何實現WordPress博客文章正文外鏈無插件代碼自動轉內鏈教程》
文章鏈接:http://www.lrracing.com/21311.html
【聲明】:國外主機測評僅分享信息,不參與任何交易,也非中介,所有內容僅代表個人觀點,均不作直接、間接、法定、約定的保證,讀者購買風險自擔。一旦您訪問國外主機測評,即表示您已經知曉并接受了此聲明通告。
【關于安全】:任何 IDC商家都有倒閉和跑路的可能,備份永遠是最佳選擇,服務器也是機器,不勤備份是對自己極不負責的表現,請保持良好的備份習慣。