WordPress 如何禁止搜索结果页被收录

在排查一位客户 Google Translate API 费用突然增长的过程中,发现站点可能遭受了 “站内搜索垃圾信息攻击” 简而言之是向 WordPress 站内搜索提交垃圾信息,如果搜索结果页被收录,垃圾信息便有被收录和被抓取到别的地方的可能。

防止页面被收录,通常有两种方法,使用 robots.txt 或者 Robots Meta,robots.txt 是一个放在网站根目录下的 txt 文件,告诉搜索引擎什么网址想被抓取,什么网址不想被抓。

禁止搜索引擎抓取站内搜索结果页的 robots.txt 写法是:

User-agent: *
Disallow: /*?s=
Disallow: /search/

Meta robots 则是向网页中加入形如

<meta name='robots' content='index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1' />

的代码,这段代码对于 WordPress 来说通常由 SEO 插件控制。

总结一下各个 SEO 插件中设置禁止收录搜索结果页的方法:

Yoast

Yoast 没有此设置项,它默认就禁止了。

<meta name='robots' content='noindex, follow' />

假如你想开放收录怎么办呢?

add_filter( 'wpseo_robots', 'my_robots_func' );
function my_robots_func( $robotsstr ) {
	if ( is_search() ) {
		return 'index,follow';		
	}
	return $robotsstr;
}

All in one SEO

All in one SEO -> Search Appearance -> Search Page -> Show in Search Results 选择No

SeoPress

SEO -> Title & Metas -> Archives -> Search archives,勾选上 Do not display search archives in search engine results (noindex)

2024-04-26_23:42:20 +0800 更新

SEOPress 7.5 开始默认禁用搜索归档

同时在 SEO -> Pro -> robots.txt 中也增加了 BLOCK SEARCH RESULTS 的快捷按钮,加入的规则如下:

user-agent: *
disallow: /?s=
disallow: /page/*/?s=
disallow: /search/

The SEO Framework

SEO -> Robots Meta Settings -> Indexing -> Global Settings,勾选 Apply noindex to Search pages? 默认已经勾选了

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

或许还会想看: