绝版电子邮箱账号
分享各类免费邮箱

WordPress文章的外链自动转为内链

WordPress是现在网站搭建非常常用的一种工具,其简单易用、强大灵活的特性深受人们的喜爱。在网站建设过程中,外链与内链都是非常重要的概念。外链指的是从其他网站链接到你的网站,而内链则是指在你的网站内部链接到其他页面或文章。

在很多情况下,用户在阅读网页时,需要点击链接跳转到其他页面,如果这些链接是外链,可能会出现跳转慢或无法打开的情况。将外链转为内链。有利于提高用户体验和网站的SEO优化。

首先,在你当前使用主题的 functions.php 中加入以下代码:

/*文章外链转内链伪静态版 
此为新窗口打开
*/  
add_filter( 'the_content', 'link_jump', 999 );
function link_jump( $content ) {
    // Get the host of the current site
    $host = parse_url( home_url(), PHP_URL_HOST );

    // Find all links in the content
    preg_match_all( '/<a[^>]+href=[\"\']([^\"\']+)[\"\'][^>]*>(.*?)<\/a>/i', $content, $matches, PREG_SET_ORDER );

    // Iterate over each link
    foreach ( $matches as $match ) {
        $url = $match[1];
        $text = $match[2];

        // Check if the link is external
        if ( strpos( $url, 'http' ) === 0 && strpos( $url, $host ) === false ) {
            // Add nofollow and target="_blank" attributes, and convert to a redirect link
            $redirect_link = home_url() . '/go.php?' . base64_encode( $url );
            $new_link = sprintf( '<a href="%s" rel="nofollow" target="_blank">%s</a>', $redirect_link, $text );
            $content = str_replace( $match[0], $new_link, $content );
        }
    }
    return $content;
}

然后在网站根目录下新建个名为 go 的文件夹,在其中写个 index.php 的文件,内容如下(请保存为UTF-8无BOM格式):

<?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=="kanyouxiang" ) {
   $t_url="//kanyouxiang.com";
} 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='https://'.$t_url;
            $title='页面加载中,请稍候...';
        } else {
            $url = 'https://'.$_SERVER['HTTP_HOST'];
            $title='参数错误,正在返回首页...';
        }
    }  

} else {
    $title = '参数缺失,正在返回首页...';
    $url = 'https://'.$_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="https://" + 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版本

rewrite ^/go/(.*)$ /go.php?url=$1 last; #注意go.php的实际路径,默认为网站根目录

尽管代码中已经用 base64 将源链接加密,并且加上了 nofollow,但恐怕蜘蛛还是能爬行,所以干脆一不做二不休,在 Robot s 禁止所有蜘蛛爬行 /go?url 目录吧!

Disallow: /go/ 
Disallow: /go?url
赞(0)
温馨提示:邮箱早期是否注册或订阅一些网站平台请自行确认。详情>>
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《WordPress文章的外链自动转为内链》
文章链接:https://kanyouxiang.com/blog/wordpress-link/
如文章来自网络,仅代表作者观点,不代表本站立场。若本站内容图文侵犯了原著者的合法权益,请联系网站删除处理。

如未标明出处,所有文章均为本站原创,如需转载,请附上原文地址,感谢您的支持和关注。

邮箱本无价 拥有自权衡

欢迎广大邮箱爱好者一起交流晒号,网站分享的邮箱理论上是永久免费个人版邮箱号!
绝版老号,无使用记录!

邮箱分类 改密帮助