电脑html外链加密,WordPress将网站内的外链转为内链并用base64加密跳转

这种方法是采用https://maosiji.com/goto/base64加密码的形式进行外链跳转。

分为两步:

一、显示的时候,用正则以及wp的钩子修改外链形式为 /goto/base64。

二、点击的时候,解密/base64部分,用template_redirect钩子和wp_redirect函数进行302跳转。

在functions.php里添加:

function wpmi_cn_convert_to_internal_links($content){

preg_match_all('/\shref=(\'|\")(http[^\'\"#]*?)(\'|\")([\s]?)/',$content,$matches);

if($matches){

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

if(strpos($val,home_url())===false){

$rep = $matches[1][0].$val.$matches[3][0];

$new = '"'.home_url().'/goto/'.base64_encode($val).'" target="_blank"';

$content = str_replace("$rep","$new",$content);

}

}

}

return $content;

}

add_filter('the_content','wpmi_cn_convert_to_internal_links',99); // 文章正文外链转换

add_filter('comment_text','wpmi_cn_convert_to_internal_links',99); // 评论内容的链接转换

add_filter('get_comment_author_link','wpmi_cn_convert_to_internal_links',99); // 访客的链接转换

function wpmi_cn_redirect() {

$baseurl = 'goto';

$request = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

$hop_base = trailingslashit(trailingslashit(home_url()).$baseurl); // 删除末尾的 斜杠/ 符号

if (substr($request,0,strlen($hop_base)) != $hop_base) return false;

$hop_key = str_ireplace($hop_base, '', $request);

if(substr($hop_key, -1) == '/')$hop_key = substr($hop_key, 0, -1);

if (!empty($hop_key)) {

$url = base64_decode($hop_key);

wp_redirect( $url, 302 );

exit;

}

}

add_action('template_redirect','wpmi_cn_redirect');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值