VPS/云主机等
优惠信息分享

禁用wordpress头部返回信息中的shortlink、RESTAPI功能且移除wp-json

这两天发现大鸟博客的百度收录中发现“?p=6235”这种形式的收录,查找了下问题所在,发现原来是wordpress头部返回信息中的shortlink,继而又发现head中有wp-json链接,具体的形式如图:

这篇文章就谈谈如何去除这2个讨人嫌的东东,特别是shortlink,导致百度收录了“?p=6235”这种动态页面地址。如图:

1、去除shortlink

我们在主题中的functions.php加入一些去掉该信息的代码就OK了。具体代码如下:

// 移除头部 wp-json 标签和 HTTP header 中的shortlinkremove_action('wp_head','wp_shortlink_wp_head',10,0); remove_action('template_redirect','wp_shortlink_header',11,0);

保存之后我们刷新网页可以看到结果,如图:

现在就只剩一个wp-json,接着看下面!

2、去除wp-json

依然是编辑主题中的functions.php,然后加入以下代码:

// 移除头部 wp-json 标签和 HTTP header 中的 linkremove_action('wp_head', 'rest_output_link_wp_head', 10 );remove_action('template_redirect', 'rest_output_link_header', 11 );

保存之后,我们看到问题也得到了解决,如图:

3、屏蔽 REST API

还是编辑functions.php文件,添加以下代码:

//屏蔽 REST APIadd_filter('json_enabled', '__return_false' );add_filter('json_jsonp_enabled', '__return_false' );add_filter('rest_enabled', '__return_false');add_filter('rest_jsonp_enabled', '__return_false');

4、安装插件

如果要解决wp-json还可以安装插件:Disable REST API

插件地址:https://wordpress.org/plugins/disable-json-api/

5、最后

去除shortlink、REST API功能且移除wp-json之后,head信息中就干净了很多,删除之后,大鸟观察了下百度也没有收录这类“?p=6235”链接了,应该说问题得到了解决。

赞(0)
未经允许不得转载:遗弃你|YIQINI.COM » 禁用wordpress头部返回信息中的shortlink、RESTAPI功能且移除wp-json

评论 抢沙发

评论前必须登录!

 

登录

找回密码

注册