为你的 WordPress 站点添加 robots.txt

稍微接触过SEO的站长朋友都应该知道 robots协议(也称为爬虫协议、爬虫规则、机器人协议等),也就是通常添加到网站根目录的robots.txt 文件,它的作用在于告诉搜索引擎哪些页面可以抓取,哪些页面不能抓取,从而优化网站的收录结果和权重。

如果你的网站根目录还没有 robots.txt,你可以创建一个,具体的写法请参考 百度百科。下面是一个基本的 WordPress  robots协议:

User-agent: *
Disallow: /feed/
Disallow: /trackback/
Disallow: /wp-admin/
Disallow: /wp-content/
Disallow: /wp-includes/
Disallow: /xmlrpc.php
Disallow: /wp-
Allow: /wp-content/uploads/
Sitemap: http://example.com/sitemap.xml

下面主要介绍一下 WordPress 自动创建虚拟 robots.txt 功能。如果你的网站根目录不存在真实的 robots.txt,那么可以让 WordPress 自动创建一个虚拟的 robots.txt (该文件不会真实存在,但是访问 http://yoursite.com/robots.txt 时可以正常显示)

将下面的代码添加到主题的 functions.php 即可:

/**
 * 为你的 WordPress 站点添加 robots.txt
 * https://www.wpdaxue.com/add-robots-txt.html
 */
add_filter( 'robots_txt', 'robots_mod', 10, 2 );
function robots_mod( $output, $public ) {
	$output .= "Disallow: /user/"; // 禁止收录链接中包含 /user/ 的页面
	return $output;
}

注:如果你想添加更多的规则,请复制上面代码的第 7 行,然后修改一下即可。

访问 http://yoursite.com/robots.txt  我们可以看到如下内容:

User-agent: *
Disallow: /wp-admin/
Disallow: /wp-includes/
Disallow: /user/

也就是说,WordPress 默认已经添加了前 3 行规则了。

robots.txt 涉及到网站的收录,所以请确保你明白它的书写方法,保证每条规则都是正确无误的!

来源:

https://www.wpdaxue.com/add-robots-txt.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_33467.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?