tiger-sms ChatGPT 账号/ChatGPT 代注册 OpenAI API 代充值

WordPress 非插件版添加 nofollow 标签与外链跳转页面

dajiaka OpenAI API key

为了防止网站权重的流失,我们可以为外链添加 nofollow 标签,高速搜索引擎不要追踪此链接,除此之外,我们也可以加入外链跳转页面实现外链转内链并修改 robots 文件,更彻底的放债网站权重的流失。本文就分享一个不使用插件来实现这个目的的方法。

一、下载外链跳转页面

外链跳转页面是指当从你的网站 / 博客打开一个不是你博客的链接时的跳转页面,这里分享一个老王博客使用的外链跳转页面,具体效果如下:便宜VPS网

这个外链跳转页面的下载地址:flyzy小站下载站 -> 建站 -> go.zip,下载好后放入网站根目录即可。

二、外链转内链

自动为 WordPress 的所有外链都添加 nofollow 标签和 go 外链跳转,只需要在你的 functions.php 文件里加入以下代码:

add_filter('the_content','the_content_nofollow',999);
function the_content_nofollow($content) {
    preg_match_all('/<a(.*?)href="(.*?)"(.*?)>/',$content,$matches);
    if($matches){
        foreach($matches[2] as $val){
            if(strpos($val,"#")===false) {
                $content = str_replace( "href=\"".$val."\"", " target=\"_blank\" "."href=\"".$val."\"", $content );
            }
            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\"", " rel=\"nofollow\" href=\"".home_url()."/go/?url=$val\" ",$content);
            }
        }
    }
    return $content;
}

这个代码会忽略内链,将外链自动添加 nofollow 标签和 go 跳转,并将所有链接都通过新标签页的方式打开。

三、修改 robots 文件

robots.txt 是告诉搜素引擎哪些页面抓取,哪些页面不应该抓取的。因为我们的外链都是通过 your_domain/go/ 这样的方式打开的,所以我们要禁止抓取 /go/,在你的 robots.txt 加入以下代码:

Disallow: /go/
赞(0)
关注我们
未经允许不得转载:老王博客 » WordPress 非插件版添加 nofollow 标签与外链跳转页面