WordPress如何实现外链加密及伪静态跳转?

无评论

作者照片

By 霜天

作为站长,都需要了解WordPress网站外链转内链的优化代码,今天就特意为大家分享一下WordPress如何实现外链加密及伪静态跳转?外链转内链跳转地址为 Base64 加密地址的详细步骤:

1、将以下跳转代码保存为 index.php,然后在网站根目录新建一个 go 文件夹,并把 index.php 上传到 go 文件夹中:

<?php

if(strlen($_SERVER[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’REQUEST_URI\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’]) > 255 ||

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==\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”yigujin\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\” ) {

$t_url=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”https://www.yigujin.cn\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”;

} elseif($t_url==\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”nana\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”) {

$t_url=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”https://www.yigujin.cn/nana\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”;

}

//数据处理

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=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”https://www.wpzt.net/<?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>

其中第行代码中自定义的一些特别外链,这个功能特别好,因为我们每个站点都或多或少有一些特殊的外链,如果也变成 Base64 加密地址就不好看了,所以此处我们可以多添加几个比较有个性化的外链跳转地址。

2、将以下代码添加到我们的 Nginx 服务器相应配置文件中的 location / { 前面,保存配置文件后平滑重启 Nginx 即可:

rewrite ^/goto/(.*)$ /go/?url=$1 last;

3、替换文章原外链跳转地址格式(/go/?url=外链)变更为新的外链格式(/goto/ base64 加密串),我们只需要将以下代码代替主题文件的 functions.php 文件中原先给外部链接加上跳转的代码即可:

//文章外链跳转伪静态版

add_filter(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’the_content\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’link_jump\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,999);

function link_jump($content){

preg_match_all(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’/<a(.*?)href=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”https://www.wpzt.net/(.*?)\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”(.*?)>/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,$content,$matches);

if($matches){

foreach($matches[2] as $val){

if(strpos($val,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’://\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’)!==false && strpos($val,home_url())===false && !preg_match(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\.(jpg|jepg|jpg|ico|bmp|gif|tiff)/i\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,$val) && !preg_match(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’/(ed2k|thunder|Flashget|flashget|qqdl):\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\//i\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,$val)){

$content=str_replace(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”href=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”$val\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”href=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”.home_url().\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”/goto/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”.base64_encode($val).\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”,$content);

}

}

}

return $content;

}

4、对于评论外链,是不需要改变成 base64 加密串的,保持原汁原味会比较好,所以本站就没有做评论外链跳转的相关操作,所以这步更变评论外链的步骤直接忽略。

5、将下载原外链跳转地址格式(/go/?url=外链)变更为新的外链格式(/goto/ base64 加密串),我们首先将以下代码添加到主题文件 functions.php 文件最后一个?>(记得修改代码中的 boke112.com 为自己的域名)。

// 下载外链跳转

function links_nofollow($url) {

if(strpos($url,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’://\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’)!==false && strpos($url,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’boke112.com\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’)===false && !preg_match(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’/(ed2k|thunder|Flashget|flashget|qqdl):\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\//i\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,$url)) {

$url = str_replace($url, home_url().\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”/goto/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”.base64_encode($url),$url);

}

return $url;

}

然后将主题文件中关于下载链接的地址改为以下代码即可:

<?php echo links_nofollow($url);?>

boke112 导航的下载链接文件就在 inc\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\file.php(其他主题请自行寻找),然后将四个下载链接地址分别改成上述相对应的代码即可,如将第一个下载链接地址:

<?php echo $url1; ?>

改为:

<?php echo links_nofollow($url1);?>

至此,外链及下载地址均变成新的跳转格式(/goto/ base64 加密串),最难得的是原旧跳转格式(/go/?url=外链)依然有效,这个才是值得点赞的。

发表评论