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

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

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

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

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

1、去除shortlink

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

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

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

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

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

2、去除wp-json

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

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

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

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

3、屏蔽 REST API

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

//屏蔽 REST API
add_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/

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

5、最后

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


【AD】DMIT美国洛杉矶Pro VPS套餐,$14.9/月起,1G-10Gbps带宽/CN2 GIA线路/有高防GIA可选
【AD】美国洛杉矶CN2 VPS/香港CN2 VPS/日本CN2 VPS推荐,延迟低、稳定性高、免费备份_搬瓦工vps