WordPress为外链添加跳转页面

这东西可以优化SEO,喜欢的朋友可以试试,方法不复杂,个人进行了修改,主要是提供了一个仿QQ邮箱外链跳转的页面,如下图:

制作跳转页面

首先新建一个page-goto.php文件(UTF-8无bom格式),加入以下代码:(注意修改注释说明的部分为你对应的信息)

<?php
    /*
    Template Name: 外链跳转
    */
?>
<?php
$home = "http://127.0.0.1";    //修改成自己的网址
$name = "INLOJV";    //修改成自己的网站名
function be_referer($home,$name) {
    echo '<!DOCTYPE html PUBLIC "-//W3C//Dtd XHTML 1.0 transitional//EN" "http://www.w3.org/tr/xhtml1/Dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta charset="UTF-8" /><meta http-equiv=Content-Type content="text/html;charset=utf-8">
<title>无效地址</title></head>
    <body>
    <div style="text-align:center;background:#eee;padding:50px 0;margin:0 auto">
    <p style="font-size:30px">抱歉,此URL不允许直接复制打开!!!</p>
    <p style="font-size:30px">—— 来自<a href="'.$home.'/">'.$name.'</a></p>
    </div>
    </body>
    </html>';
    exit();
}
// 判断:若不为跳转链接,则不执行跳转,显示以上信息
if(empty($_SERVER['HTTP_REFERER']) || strpos(strtoupper($_SERVER['HTTP_REFERER']),strtoupper($home)) === false){
    be_referer($home,$name);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//Dtd XHTML 1.0 transitional//EN" "http://www.w3.org/tr/xhtml1/Dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<meta name="robots" content="noindex,nofollow">
<title>INLOJV跳转页面··· | <?php echo $name;?></title>
<!-- 原创样式 BY:INLOJV -->
<style>
body {font:15px  Arial, Lucida Grande, Tahoma, sans-serif;background: #fff;}
h2{font-size:18px;color:#ddd}
a {font-size:16px;color:#555;text-decoration: none;}
span {color:red;}
#wrapper {width:700px;margin:10px auto;padding:5px;}
#content {background:#fff;margin:10px 0 20px 0;border:1px solid #BBB;box-shadow:0px 0px 3px #D4D4D4;}
.c-container {padding: 30px;}
.remind_icon {float: left;margin-right: 10px;display: inline;width: 32px;height: 32px;background: url('http://rescdn.qqmail.com/zh_CN/htmledition/images/newicon/prompt1ac0c0.png') no-repeat scroll 0% 0% transparent;background-position:-64px 0px;}
.remind_content {overflow: hidden;}
.remind_title {color:#D68300;margin-bottom: 10px;padding-top: 3px;font-weight: bold;font-size: 20px;font-family: "Microsoft YaHei","lucida Grande",Verdana;}
.remind_detail {line-height:1.5;font-size:15px;color: #535353;}
.safety-url {margin-bottom:15px;padding-bottom: 15px;border-bottom: 1px solid #DFDFDF;word-wrap: break-word;word-break: break-all;}
.c-footer {padding: 10px 15px;background: none repeat scroll 0% 0% #F1F1F1;border-top: 1px solid #BBB;overflow: hidden;}
a.btn_blue {border: 1px solid #0D659B;color: #FFF !important;background: -moz-linear-gradient(center top , #238ACA, #0074BC) repeat scroll 0% 0% transparent;display: inline-block;padding: 6px 25px;margin: 0px;font-size: 14px;font-weight: bold;text-align: center;border-radius: 3px;}
.c-footer-a2 {margin: 8px 0px 0px 15px;}
</style>
</head>
<body>
<div id="wrapper">
    <h2>INLOJV 跳转页面</h2>
    <div id="content">
    <div class="c-container">
        <span class="remind_icon"></span>
        <div class="remind_content">
            <div class="remind_title">您将要访问:</div>
            <div class="remind_detail">
                <div class="safety-url">
                    <span ><a href="<?php $url=$_GET['url']; echo base64_decode($url);?>" rel="nofollow" ><?php $url=$_GET['url']; echo base64_decode($url);?></a></span><br/><br/><span id="secondsDisplay">5</span> 秒后开始跳转...<br/>                    
                </div>
                <p>建议您不要在该网页输入QQ密码、手机号码等个人私密信息。 </p>
                <p>该链接可能超出本站范围,请您决定是否转向,<b>5</b> 秒后会自动打开访问链接。</p>    
            </div>
        </div>
    </div>
    <div class="c-footer">
        <a href="<?php $url=$_GET['url']; echo base64_decode($url);?>" rel="nofollow" class="c-footer-a1 btn_blue">继续访问</a>
        <a href="http://www.inlojv.com" class="c-footer-a2">返回 INLOJV</a>
                
    </div>
    </div>
<script type="text/javascript">
    var i = 5; // 修改等待时间,默认5秒
    var intervalid;
    intervalid = setInterval("fun()", 1000);
    function fun() {
        if (i == 0) {
            window.location.href = "<?php $url=$_GET['url']; echo base64_decode($url);?>";
            clearInterval(intervalid);
        }
    document.getElementById("secondsDisplay").innerHTML = i;
    i--;
    }
</script>
</div>
</body>
</html>

这样跳转页面做好了,保存为page-goto.php丢到主题目录下,在wp后台新建一个页面选择“外链跳转”模板,固定链接改为http://127.0.0.1/goto 确定。如此跳转页的链接就是http://127.0.0.1/goto了。

评论者的链接转内链

剩下就是改URL,喜欢改哪里就改哪里,比如本站仅对评论区的访客链接进行了修改,修改href里面的链接格式,其中有这么一句:

href="'. $comment->comment_author_url. '"

改为

href="'.get_bloginfo('home').'/goto?url='.base64_encode($comment->comment_author_url).'"

当然,你也可以用wp的函数钩子来替换评论者的链接! 改好之后,点击访客链接,就会显示成类似
http://www.inlojv.com/goto.php?url=访客链接 这样的跳转链接。
注意:为了安全起见,外链url是经过base64加密的!

文章的外链转内链

如果要把文章主体的外链也转成内链,需要再写一个钩子,添加到functions.php里面,代码如下

// 文章外链转内链并转入跳转页面
add_filter('the_content','goto_url',999);
function goto_url($content){
    preg_match_all('/href="(.*?)"/',$content,$matches);
    if($matches){
        foreach($matches[1] as $val){
            if( strpos($val,home_url())===false&&strpos($val,"javascript:void(0)")===false )
                $content=str_replace("href=\"$val\"", "rel=\"nofollow\" target=\"_blank\" href=\"" . get_bloginfo('home'). "/goto?url=" .base64_encode($val). "\"",$content);
        }
    }
    return $content;
}

修改robots

最后一步,不要忘了在robots.txt中添加一句:

Disallow: /goto.php?*

要禁止蜘蛛来爬这样的跳转链接,否则就前功尽弃了。下面提供page-go.php源文件:

page-goto.php下载地址

page-goto

page-go.php代码:

<?php
	/*
	Template Name: 外链跳转
	*/
?>
<?php
$home = "http://127.0.0.1";    //修改成自己的网址
$name = "INLOJV";    //修改成自己的网站名
function be_referer($home,$name) {
    echo '<!DOCTYPE html PUBLIC "-//W3C//Dtd XHTML 1.0 transitional//EN" "http://www.w3.org/tr/xhtml1/Dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta charset="UTF-8" /><meta http-equiv=Content-Type content="text/html;charset=utf-8">
<title>无效地址</title></head>
    <body>
	<div style="text-align:center;background:#eee;padding:50px 0;margin:0 auto">
    <p style="font-size:30px">抱歉,此URL不允许直接复制打开!!!</p>
    <p style="font-size:30px">—— 来自<a href="'.$home.'/">'.$name.'</a></p>
	</div>
    </body>
    </html>';
    exit();
}
// 判断:若不为跳转链接,则不执行跳转,显示以上信息
if(empty($_SERVER['HTTP_REFERER']) || strpos(strtoupper($_SERVER['HTTP_REFERER']),strtoupper($home)) === false){
    be_referer($home,$name);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//Dtd XHTML 1.0 transitional//EN" "http://www.w3.org/tr/xhtml1/Dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<meta name="robots" content="noindex,nofollow">
<title>INLOJV跳转页面··· | <?php echo $name;?></title>
<!-- 原创样式 BY:INLOJV -->
<style>
body {font:15px  Arial, Lucida Grande, Tahoma, sans-serif;background: #fff;}
h2{font-size:18px;color:#ddd}
a {font-size:16px;color:#555;text-decoration: none;}
span {color:red;}
#wrapper {width:700px;margin:10px auto;padding:5px;}
#content {background:#fff;margin:10px 0 20px 0;border:1px solid #BBB;box-shadow:0px 0px 3px #D4D4D4;}
.c-container {padding: 30px;}
.remind_icon {float: left;margin-right: 10px;display: inline;width: 32px;height: 32px;background: url('http://rescdn.qqmail.com/zh_CN/htmledition/images/newicon/prompt1ac0c0.png') no-repeat scroll 0% 0% transparent;background-position:-64px 0px;}
.remind_content {overflow: hidden;}
.remind_title {color:#D68300;margin-bottom: 10px;padding-top: 3px;font-weight: bold;font-size: 20px;font-family: "Microsoft YaHei","lucida Grande",Verdana;}
.remind_detail {line-height:1.5;font-size:15px;color: #535353;}
.safety-url {margin-bottom:15px;padding-bottom: 15px;border-bottom: 1px solid #DFDFDF;word-wrap: break-word;word-break: break-all;}
.c-footer {padding: 10px 15px;background: none repeat scroll 0% 0% #F1F1F1;border-top: 1px solid #BBB;overflow: hidden;}
a.btn_blue {border: 1px solid #0D659B;color: #FFF !important;background: -moz-linear-gradient(center top , #238ACA, #0074BC) repeat scroll 0% 0% transparent;display: inline-block;padding: 6px 25px;margin: 0px;font-size: 14px;font-weight: bold;text-align: center;border-radius: 3px;}
.c-footer-a2 {margin: 8px 0px 0px 15px;}
</style>
</head>
<body>
<div id="wrapper">
    <h2>INLOJV 跳转页面</h2>
	<div id="content">
	<div class="c-container">
		<span class="remind_icon"></span>
		<div class="remind_content">
			<div class="remind_title">您将要访问:</div>
			<div class="remind_detail">
				<div class="safety-url">
					<span ><a href="<?php $url=$_GET['url']; echo base64_decode($url);?>" rel="nofollow" ><?php $url=$_GET['url']; echo base64_decode($url);?></a></span><br/><br/><span id="secondsDisplay">5</span> 秒后开始跳转...<br/>					
				</div>
				<p>建议您不要在该网页输入QQ密码、手机号码等个人私密信息。 </p>
				<p>该链接可能超出本站范围,请您决定是否转向,<b>5</b> 秒后会自动打开访问链接。</p>	
			</div>
		</div>
	</div>
	<div class="c-footer">
		<a href="<?php $url=$_GET['url']; echo base64_decode($url);?>" rel="nofollow" class="c-footer-a1 btn_blue">继续访问</a>
		<a href="http://www.inlojv.com" class="c-footer-a2">返回 INLOJV</a>
				
	</div>
	</div>
<script type="text/javascript">
    var i = 5; // 修改等待时间,默认5秒
    var intervalid;
    intervalid = setInterval("fun()", 1000);
    function fun() {
        if (i == 0) {
            window.location.href = "<?php $url=$_GET['url']; echo base64_decode($url);?>";
            clearInterval(intervalid);
        }
    document.getElementById("secondsDisplay").innerHTML = i;
    i--;
    }
</script>
</div>
</body>
</html>