Sub topic 8
Writing the robots.txt File
This chapter assumes you understand HTTP requests, the User-Agent header, basic web crawlers, and how to edit files served from a website's root. You should also be comfortable running curl and reading simple Python code.
Start with a crawler policy
A robots.txt file implements a policy for automated clients. Decide the policy before writing rules:
- Block training crawlers if you do not want their datasets to include your pages.
- Allow AI search crawlers if you want your pages to be discoverable in AI-generated search results.
- Allow user-initiated fetchers if an assistant may retrieve a page after a person asks for it.
- Apply a separate policy to each crawler where possible.
These purposes are different. For example, GPTBot is associated with OpenAI's training crawler, while OAI-SearchBot supports search. ChatGPT-User fetches pages in response to user requests. Blocking one token does not automatically block the others.
Build groups with User-agent and Disallow
Each group begins with one or more User-agent lines. The directives that follow belong to that group until the next User-agent line.
Disallow: / blocks every path. Disallow: /members/ blocks paths beginning with /members/, while leaving other paths available. An empty Disallow value allows crawling.
The following policy blocks common training-related crawlers, but allows OpenAI search, user-request fetches, and other unidentified crawlers:
# Training crawlers
User-agent: GPTBot
Disallow: /
User-agent: Google-Extended
Disallow: /
User-agent: ClaudeBot
Disallow: /
User-agent: CCBot
Disallow: /
User-agent: Applebot-Extended
Disallow: /
# OpenAI search and user-request crawlers
User-agent: OAI-SearchBot
Allow: /
User-agent: ChatGPT-User
Allow: /
# Other crawlers, including PerplexityBot unless separately listed
User-agent: *
Allow: /Google-Extended is a Google policy token. It controls the use of content for certain generative AI training and grounding purposes. It does not control Google Search crawling, indexing, or ranking. A Google-Extended rule therefore does not replace a Googlebot rule.
Match rules deliberately
A crawler selects the most specific matching User-agent group. If no named group matches, it uses the * group. Within the selected group, matching path rules are compared by path length. The longest matching rule wins. If equally long Allow and Disallow rules conflict, Allow wins.
For example:
User-agent: GPTBot
Disallow: /private/
Allow: /private/public-guide.htmlThis blocks the /private/ directory except for the longer, explicitly allowed file path. Rules in a different group do not combine with this group. A GPTBot request does not take a Disallow from the wildcard group and add it to the named group.
Use path-level rules when needed
A site may want to block memberships and drafts from training while allowing public articles:
User-agent: GPTBot
Disallow: /members/
Disallow: /drafts/
User-agent: Google-Extended
Disallow: /members/
Disallow: /drafts/
User-agent: *
Allow: /A path rule is not access control. It does not protect confidential information. Require authentication for private content, even if the path appears in robots.txt.
Visualize the decision
Test before publishing
Python's standard library can evaluate a draft:
from urllib.robotparser import RobotFileParser
rules = """\
User-agent: GPTBot
Disallow: /
User-agent: OAI-SearchBot
Allow: /
User-agent: *
Allow: /
"""
parser = RobotFileParser()
parser.parse(rules.splitlines())
url = "https://example.com/articles/guide"
for agent in ["GPTBot", "OAI-SearchBot", "SomeOtherBot"]:
print(agent, parser.can_fetch(agent, url))Expected output is:
GPTBot False
OAI-SearchBot True
SomeOtherBot TrueAfter deployment, verify the file that the server actually returns:
curl -sI https://example.com/robots.txt | head -n 5
curl -s https://example.com/robots.txtCheck the status, content type, redirects, and contents. A correct local file is irrelevant if the production server serves an old copy, redirects incorrectly, or returns an error.
Exercise
You want to block ClaudeBot from /drafts/ but allow it to crawl every other path. What group should you add?
Show answer
Add User-agent: ClaudeBot followed by Disallow: /drafts/. Do not use Disallow: /, because that would block every path.