将Wordpress文章的外链转为内链(NGINX版)

道锋潜鳞
2020-12-23 / 0 评论 / 54 阅读 / 正在检测是否收录...

我们在wordpress网站很多的文章需要跳转外链的资料、或者跳转外链的下载,所以在维护的时候,文章内编写的外链是不可避免的。

一般来说,我们在除了友情链接之外的外部连接之外,大部分的外链都会多多少少的分散网站的权重

你或许看见过类似https://www..com/goto.php?https://www.***.com形式的跳转链接,这样是为了站点的SEO能够对各种搜索引擎更友好,术语好像就是叫做外链跳转。更重要的是起到了保护自己域名权重的目的。

一、给出NGINX跳转版的配置方法

首先打开你站点的NGINX配置文件(仅限使用NGINXweb服务的道友)

在location的配置块里面,添加一个新的location块

代码如下

location ~ ^/goto/* {
      if ($request_method != GET) {
        return 403;
      } 
      rewrite ^ $arg_url? redirect;
    }

这段配置文件就能实现将参数url后的地址读取后302跳转,并且抛弃除GET外的请求

二、如果您用得是apache等类型的服务器

可以在根目录下新建goto文件夹,在里面新建一个index.php文件(当然其他的的地址也是可以,需要在第三框的步骤中修改成你的地址)

添加如下内容:

<?php header("location:".$_GET["url"]); ?>

美观一点也是可以的:

    <?php $url = $_GET['url']; ?>
    <html>
    <head>
    <meta charset=utf-8 />
    <meta name="robots" content="nofollow">
    <meta http-equiv="refresh" content="0.1;url=<?php echo $url; ?>">
    <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>

三、主题文件配置

随后打开你的主题内的functions.php文件。

在末尾添加如下php代码:

function the_content_nofollow($content){
    preg_match_all('/<a(.*?)href="(.*?)"(.*?)>/',$content,$matches);
    if($matches){
        foreach($matches[2] as $val){
            if(strpos($val,'://')!==false && strpos($val,home_url())===false && !preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i',$val)){
            $content=str_replace("href=\"$val\"", "href=\"".home_url()."/goto?url=$val\" ",$content);
        }
    }
}
return $content;
}
add_filter('the_content','the_content_nofollow',999);

这段代码将捕获页面链接标签内的url地址,然后进行替换操作

至此,配置完成,不出意外的话,文章内的站外链接会变成这样:

0

评论 (0)

取消