/robots-txt
生成或优化 robots.txt 文件
$ npx -y skills add huifer/claude-code-seo --agent claude-codeHow it fires
How this command gets triggered: by you, by Claude, or both.
- Fires itselfClaude auto-loads it when your prompt matches the work.
- You can call itInvoke it directly when you want it.
- Slash command
/robots-txt
Context preview
What this command does when you run it.
生成或优化 robots.txt 文件
Command definition
robots-txt.mddescription: 生成或优化 robots.txt 文件
argument-hint: [domain]
为 Next.js 项目生成 SEO 友好的 robots.txt 文件。包含基本的爬虫规则、Sitemap 引用和常见目录的禁止规则。
功能
- ✅ 生成标准的 robots.txt 文件
- ✅ 自动包含 Sitemap 引用
- ✅ 禁止敏感目录和文件
- ✅ 允许所有主要爬虫
- ✅ 支持爬虫延迟设置
- ✅ 适配 Next.js 项目结构
参数
- `$1` 或 `$ARGUMENTS`: 网站域名(可选)
- 用于生成 Sitemap URL
- 示例:`https://yourdomain.com`
- 如果不提供,Sitemap 行会被注释或省略
使用示例
示例 1:基本用法
/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 自动生成
如果你使用 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 文件必须位于网站根目录
- 文件名必须小写:`robots.txt`
- 文件更新后可能需要几天才能被搜索引擎重新抓取
- 使用 Google Search Console 的 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:**
- 允许主 feed 被抓取
- 禁止其他 feed 变体
**防止爬虫陷入无限循环:**
- 禁止带参数的搜索 URL
- 禁止深层分页
### 示例 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
电商特定规则说明
**禁止结账流程页面:**
- /checkout/, /cart/ 等页面不需要被索引
**禁止搜索和过滤 URL:**
- 避免重复内容
- 节省爬虫预算
**允许产品图片:**
- 图片搜索流量有价值
**阻止 SEO 工具爬虫:**
- SemrushBot, AhrefsBot 等可能消耗服务器资源
**多个 Sitemap:**
- 电商网站通常有多个 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
多语言 Sitemap 策略
**选项 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 单独引用**
- 在 robots.txt 中列出所有语言的 sitemap
- 确保每个语言的 sitemap URL 正确
## 高级配置
### Crawl Delay(爬虫延迟)
```txt
User-agent: *
Crawl-delay: 1
# 或针对特定爬虫
User-agent: Googlebot
Crawl-delay: 0
User-agent: Bingbot
Crawl-delay: 1
**说明:**
- 设置爬虫请求之间的延迟(秒)
- Google 通常忽略 crawl-delay
- 对小网站可能有用
阻止特定爬虫
# 阻止 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/
robots.txt 语法
Read more
description: 生成或优化 robots.txt 文件 argument-hint: [domain]
为 Next.js 项目生成 SEO 友好的 robots.txt 文件。包含基本的爬虫规则、Sitemap 引用和常见目录的禁止规则。
功能
- ✅ 生成标准的 robots.txt 文件
- ✅ 自动包含 Sitemap 引用
- ✅ 禁止敏感目录和文件
- ✅ 允许所有主要爬虫
- ✅ 支持爬虫延迟设置
- ✅ 适配 Next.js 项目结构
参数
- `$1` 或 `$ARGUMENTS`: 网站域名(可选)
- 用于生成 Sitemap URL
- 示例:`https://yourdomain.com`
- 如果不提供,Sitemap 行会被注释或省略
使用示例
示例 1:基本用法
/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 自动生成
如果你使用 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 文件必须位于网站根目录
- 文件名必须小写:`robots.txt`
- 文件更新后可能需要几天才能被搜索引擎重新抓取
- 使用 Google Search Console 的 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:**
- 允许主 feed 被抓取
- 禁止其他 feed 变体
**防止爬虫陷入无限循环:**
- 禁止带参数的搜索 URL
- 禁止深层分页
### 示例 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
电商特定规则说明
**禁止结账流程页面:**
- /checkout/, /cart/ 等页面不需要被索引
**禁止搜索和过滤 URL:**
- 避免重复内容
- 节省爬虫预算
**允许产品图片:**
- 图片搜索流量有价值
**阻止 SEO 工具爬虫:**
- SemrushBot, AhrefsBot 等可能消耗服务器资源
**多个 Sitemap:**
- 电商网站通常有多个 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
多语言 Sitemap 策略
**选项 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 单独引用**
- 在 robots.txt 中列出所有语言的 sitemap
- 确保每个语言的 sitemap URL 正确
## 高级配置 ### Crawl Delay(爬虫延迟) ```txt User-agent: * Crawl-delay: 1 # 或针对特定爬虫 User-agent: Googlebot Crawl-delay: 0 User-agent: Bingbot Crawl-delay: 1
**说明:**
- 设置爬虫请求之间的延迟(秒)
- Google 通常忽略 crawl-delay
- 对小网站可能有用
阻止特定爬虫
# 阻止 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/

