生成或优化 robots.txt 文件
Generates SEO-friendly robots.txt files for Next.js projects. Use it to create proper crawler rules, block sensitive directories, and include sitemap references for better search engine indexing.
/plugin marketplace add huifer/claude-code-seo/plugin install huifer-claude-seo-assistant@huifer/claude-code-seodomain为 Next.js 项目生成 SEO 友好的 robots.txt 文件。包含基本的爬虫规则、Sitemap 引用和常见目录的禁止规则。
$1 或 $ARGUMENTS: 网站域名(可选)
https://yourdomain.com/robots-txt
输出:
# robots.txt 文件
为你的 Next.js 项目生成的 robots.txt 文件:
```txt
# Allow all crawlers
User-agent: *
Allow: /
# Disallow common Next.js and private directories
Disallow: /api/
Disallow: /_next/
Disallow: /static/
Disallow: /.well-known/
# Disallow admin and private areas (if applicable)
# Disallow: /admin/
# Disallow: /private/
# Disallow: /dashboard/
# Sitemap (uncomment and update with your domain)
# Sitemap: https://yourdomain.com/sitemap.xml
将此文件放置在项目根目录的 public/ 文件夹中:
路径: public/robots.txt
复制上面的内容到 public/robots.txt 文件。
创建后,访问 https://yourdomain.com/robots.txt 验证文件可访问。
如果你使用 Next.js 13.3+,也可以使用 generateRobotTxt 函数自动生成:
app/robots.ts (推荐):
import { MetadataRoute } from 'next'
export default function robots(): MetadataRoute.Robots {
return {
rules: [
{
userAgent: '*',
allow: '/',
disallow: ['/api/', '/_next/', '/static/'],
},
],
sitemap: 'https://yourdomain.com/sitemap.xml',
}
}
这会自动生成 /robots.txt 路由。
### 示例 2:指定域名
```bash
/robots-txt https://yourdomain.com
输出:
# robots.txt 文件(包含 Sitemap)
```txt
# Allow all crawlers
User-agent: *
Allow: /
# Disallow common Next.js directories
Disallow: /api/
Disallow: /_next/
Disallow: /static/
# Disallow private areas
Disallow: /admin/
Disallow: /_next/image/
Disallow: /private/
# Crawl delay (optional)
# Crawl-delay: 1
# Sitemap
Sitemap: https://yourdomain.com/sitemap.xml
将上述内容保存到 public/robots.txt,部署后可通过以下 URL 访问:
https://yourdomain.com/robots.txt
robots.txt
### 示例 3:博客项目
```bash
/robots-txt https://blog.yourdomain.com
输出:
# robots.txt for Blog
```txt
# Allow all crawlers
User-agent: *
Allow: /
# Disallow admin and management areas
Disallow: /admin/
Disallow: /api/
Disallow: /_next/
Disallow: /wp-admin/
Disallow: /wp-includes/
# Disallow search and filter pages (avoid duplicate content)
Disallow: /search?
Disallow: /filter?
Disallow: /tag/*/page/
Disallow: /category/*/page/
# Allow specific feeds
Allow: /feed/
Allow: /feed/rss/
# Disallow other feeds
Disallow: */feed/
Disallow: */feed/rss/
Disallow: */feed/atom/
# Sitemap
Sitemap: https://blog.yourdomain.com/sitemap.xml
禁止搜索和分页页面:
允许主要 feed:
防止爬虫陷入无限循环:
### 示例 4:电商项目
```bash
/robots-txt https://store.yourdomain.com
输出:
# robots.txt for E-commerce Store
```txt
# Allow all crawlers
User-agent: *
Allow: /
# Disallow admin and backend
Disallow: /admin/
Disallow: /api/
Disallow: /_next/
Disallow: /checkout/
Disallow: /cart/
Disallow: /my-account/
Disallow: /wishlist/
# Disallow search and filter pages
Disallow: /search?
Disallow: /catalogsearch/
Disallow: /filter?
# Disallow sorted and filtered URLs
Disallow: /*?sort=
Disallow: /*?order=
Disallow: /*?filter=
Disallow: /*?price=
# Disallow session IDs and tracking parameters
Disallow: /*?PHPSESSID=
Disallow: /*?utm_source=
Disallow: /*?utm_medium=
Disallow: /*?utm_campaign=
# Allow product images
Allow: /media/catalog/
Allow: /products/images/
# Disallow temporary files
Disallow: /tmp/
Disallow: /var/
Disallow: /cache/
# Specific crawler rules
# Block aggressive crawlers (optional)
User-agent: SemrushBot
Disallow: /
User-agent: AhrefsBot
Disallow: /
# Sitemap
Sitemap: https://store.yourdomain.com/sitemap.xml
Sitemap: https://store.yourdomain.com/sitemap-product.xml
Sitemap: https://store.yourdomain.com/sitemap-category.xml
禁止结账流程页面:
禁止搜索和过滤 URL:
允许产品图片:
阻止 SEO 工具爬虫:
多个 Sitemap:
### 示例 5:多语言项目
```bash
/robots-txt https://yourdomain.com
输出:
# robots.txt for Multi-language Site
```txt
# Allow all crawlers
User-agent: *
Allow: /
# Disallow common directories
Disallow: /api/
Disallow: /_next/
Disallow: /static/
# Sitemap for all languages
Sitemap: https://yourdomain.com/sitemap.xml
Sitemap: https://yourdomain.com/en/sitemap.xml
Sitemap: https://yourdomain.com/zh/sitemap.xml
Sitemap: https://yourdomain.com/es/sitemap.xml
选项 1:主 sitemap 引用子 sitemap
<!-- sitemap.xml -->
<sitemapindex>
<sitemap>
<loc>https://yourdomain.com/en/sitemap.xml</loc>
</sitemap>
<sitemap>
<loc>https://yourdomain.com/zh/sitemap.xml</loc>
</sitemap>
</sitemapindex>
选项 2:每个语言的 sitemap 单独引用
## 高级配置
### Crawl Delay(爬虫延迟)
```txt
User-agent: *
Crawl-delay: 1
# 或针对特定爬虫
User-agent: Googlebot
Crawl-delay: 0
User-agent: Bingbot
Crawl-delay: 1
说明:
# 阻止 SEO 工具爬虫
User-agent: SemrushBot
Disallow: /
User-agent: AhrefsBot
Disallow: /
User-agent: MJ12bot
Disallow: /
# 阻止存档网站
User-agent: archive.org_bot
Disallow: /
注意: 只阻止消耗大量资源的爬虫,不要阻止主要搜索引擎。
User-agent: *
Allow: /images/
Allow: /css/
Allow: /js/
Disallow: /api/
Disallow: /admin/
说明: 在禁止规则之前使用 Allow 规则。
# Googlebot
User-agent: Googlebot
Allow: /
Disallow: /private/
# Bingbot
User-agent: Bingbot
Allow: /
Disallow: /private/
# 其他所有爬虫
User-agent: *
Allow: /
Disallow: /private/
Disallow: /admin/
User-agent:
User-agent: * # 所有爬虫
User-agent: Googlebot # 特定爬虫
Disallow:
Disallow: / # 禁止整个网站
Disallow: /admin/ # 禁止目录
Disallow: /file.html # 禁止文件
Disallow: /*.pdf # 禁止所有 PDF
Allow:
Allow: / # 允许整个网站
Allow: /images/ # 允许目录
Sitemap:
Sitemap: https://yourdomain.com/sitemap.xml
Sitemap: https://yourdomain.com/sitemap-2.xml
Crawl-delay:
Crawl-delay: 1 # 1 秒延迟
* 匹配任意字符:
Disallow: /*? # 禁止所有带参数的 URL
Disallow: /*.pdf # 禁止所有 PDF 文件
Disallow: /private* # 禁止以 /private 开头的所有路径
$ 结束标记:
Disallow: /*.pdf$ # 禁止以 .pdf 结尾的 URL
Disallow: /print$ # 禁止以 /print 结尾的 URL
User-agent: *
Allow: /
Disallow: /admin/
大多数网站只需如此简单。
# 禁止搜索结果、过滤、排序页面
Disallow: /search?
Disallow: /filter?
Disallow: /*?sort=
Disallow: /*?order=
User-agent: *
Allow: /images/
Allow: /css/
Allow: /js/
Disallow: /api/
使用以下工具测试:
# 错误
User-agent: *
Disallow: /
修复:
# 正确
User-agent: *
Allow: /
Disallow: /admin/
# 错误 - 缺少尾部斜杠
Disallow: /admin
# 正确 - 目录应该有尾部斜杠
Disallow: /admin/
# 错误 - 阻止主要搜索引擎
User-agent: Googlebot
Disallow: /
除非有充分理由,否则不要阻止主要搜索引擎。
# 错误 - 过于复杂
User-agent: *
Allow: /images/jpeg/
Allow: /images/png/
Disallow: /images/gif/
Disallow: /images/webp/
Disallow: /images/svg/
# ... 更多规则
保持简单:
User-agent: *
Allow: /images/
Disallow: /private/
将 robots.txt 放在 public/ 目录:
public/
robots.txt
sitemap.xml
app/robots.ts 或 app/robots.js:
import { MetadataRoute } from 'next'
export default function robots(): MetadataRoute.Robots {
const baseUrl = 'https://yourdomain.com'
return {
rules: [
{
userAgent: '*',
allow: '/',
disallow: [
'/api/',
'/_next/',
'/static/',
'/admin/',
],
},
{
userAgent: ['SemrushBot', 'AhrefsBot'],
disallow: '/',
},
],
sitemap: `${baseUrl}/sitemap.xml`,
}
}
环境变量配置:
import { MetadataRoute } from 'next'
export default function robots(): MetadataRoute.Robots {
const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || 'https://yourdomain.com'
return {
rules: [
{
userAgent: '*',
allow: '/',
disallow: ['/api/', '/_next/'],
},
],
sitemap: `${baseUrl}/sitemap.xml`,
}
}
动态配置:
import { MetadataRoute } from 'next'
export default function robots(): MetadataRoute.Robots {
return {
rules: [
{
userAgent: '*',
allow: '/',
disallow: process.env.NODE_ENV === 'production'
? ['/admin/', '/private/']
: ['/'],
},
],
sitemap: 'https://yourdomain.com/sitemap.xml',
}
}
# 开发环境
curl http://localhost:3000/robots.txt
# 生产环境
curl https://yourdomain.com/robots.txt
/robots.txtrobots.txtA: 通常需要几天到一周。搜索引擎不会立即重新抓取。
A: 不要使用 robots.txt。使用:
A: 不能。禁止目录会禁止该目录下的所有资源。
A: 使用通配符:
Disallow: /*?parameter=
Disallow: /*?utm_source=
A: 通常不需要。大多数网站对所有爬虫使用相同规则。
A:
通常结合使用以获得最佳效果。
/llm-txt - 生成 llm.txt 文件/seo-audit - 检查 robots.txt 配置/seo-check - 验证 robots.txt 可访问性robots.txt