本文介绍如何通过 `.htaccess` 文件中的 `rewriterule` 规则,仅对指定子目录(如 `/tbd_templates/` 和 `/custom_post/`)返回 403 禁止访问响应,同时不影响 wordpress 正常路由和其他站点功能。
要在 WordPress 环境中精准限制爬虫(如 Googlebot、Bingbot、Baiduspider)访问特定目录(

以下是推荐配置(请插入到 # BEGIN WordPress 注释行之前):
# Block specific bots from accessing sensitive directories
RewriteCond %{HTTP_USER_AGENT} (googlebot|bingbot|Baiduspider) [NC]
RewriteRule ^(tbd_templates|custom_post)($|/) - [F]✅ 关键说明:
⚠️ 注意事项:
curl -I -A "Googlebot" https://example.com/tbd_templates/ # 应返回 HTTP/1.1 403 Forbidden curl -I -A "Mozilla/5.0" https://example.com/tbd_templates/ # 应返回 HTTP/1.1 200 OK(正常访问)
综上,该方案兼顾安全性、可维护性与兼容性,是 WordPress 站点中实现细粒度爬虫访问控制的标准实践。