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

2023-05-3010:33:07网站运营与SEO优化Comments1,059 views字数 1001阅读模式

防止网站权重的流失,我们可以为外链添加 nofollow 标签,高速搜索引擎不要追踪此链接,除此之外,我们也可以加入外链跳转页面实现外链转内链并修改 robots 文件,更彻底的放债网站权重的流失。本文就分享一个不使用插件来实现这个目的的方法。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/youhua/43414.html

一、下载外链跳转页面

外链跳转页面是指当从你的网站 / 博客打开一个不是你博客的链接时的跳转页面,这里分享一个老王博客使用的外链跳转页面。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/youhua/43414.html

二、外链转内链

自动为 WordPress 的所有外链都添加 nofollow 标签和 go 外链跳转,只需要在你的 functions.php 文件里加入以下代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/youhua/43414.html

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 跳转,并将所有链接都通过新标签页的方式打开。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/youhua/43414.html

三、修改 robots 文件

robots.txt 是告诉搜素引擎哪些页面抓取,哪些页面不应该抓取的。因为我们的外链都是通过 your_domain/go/ 这样的方式打开的,所以我们要禁止抓取 /go/,在你的 robots.txt 加入以下代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/youhua/43414.html

Disallow: /go/
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/youhua/43414.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/youhua/43414.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定