Sub topic 8
Writing the robots.txt file
This chapter turns the policy you chose in the previous chapter into a working robots.txt file. It assumes you can edit files served from your site root, read basic HTTP headers, and run curl and short Python scripts. It also assumes you know which crawlers you want to allow or block, and why.
Where the file lives and what it must look like
A crawler requests /robots.txt once per host before fetching other pages. These constraints come from the Robots Exclusion Protocol (RFC 9309):
- The file must be at the root of the host:
https://example.com/robots.txt. A copy in/blog/robots.txtis ignored. - It applies only to the exact protocol, host, and port it is served from.
https://www.example.comandhttps://blog.example.comeach need their own file. - It must be plain text encoded as UTF-8, served with a 200 status.
- Crawlers must parse at least the first 500 KiB. Keep the file far smaller than that.
What happens when the file is missing or broken matters more than most people expect:
| Response for `/robots.txt` | What a compliant crawler does |
|---|---|
| 200 with rules | Applies the rules |
| 404 or other 4xx | Treats the site as having no restrictions |
| 5xx or unreachable | Assumes everything is disallowed, at least temporarily |
A deploy that returns a 404 for the file silently allows every crawler. A server error can make crawlers back off from your whole site, including search engines.
The building blocks
A file is a list of groups. Each group starts with one or more User-agent lines, followed by rules.
User-agent: GPTBot
Disallow: /private/
Allow: /private/press/User-agentnames the crawler's product token, such asGPTBot. Matching ignores case.Disallowgives a path prefix the crawler should not fetch. An emptyDisallow:blocks nothing.Allowgives a path prefix that is permitted, usually as an exception inside a disallowed area.- Paths are case-sensitive and are matched from the start of the URL path, including the query string.
*matches any run of characters.$anchors the end of the URL. For example,Disallow: /*.pdf$blocks URLs ending in.pdf.- Lines starting with
#are comments. Blank lines separate groups.
Any path with no matching rule is allowed. You never need to write Allow: / to permit a crawler. It only makes your intent explicit.
How a crawler chooses a group
A crawler does not read the whole file top to bottom. It picks the group that names it most specifically and ignores the rest.
Two consequences matter when you write the file.
- A specific group replaces the
*group. Rules are not inherited. If*disallows/drafts/and you add aGPTBotgroup that only disallows/members/, GPTBot is free to fetch/drafts/. - Within a group, the longest matching path wins. With
Disallow: /members/andAllow: /members/public/, the URL/members/public/faqis allowed because the Allow rule is longer. If two rules match with equal length, the less restrictive one (Allow) wins.
Sharing rules across several crawlers
Consecutive User-agent lines with no rules between them form one group. This avoids repeating the same rules for every training crawler:
# Training crawlers share one rule set
User-agent: GPTBot
User-agent: Google-Extended
User-agent: ClaudeBot
User-agent: CCBot
User-agent: Applebot-Extended
Disallow: /Do not put a blank line or a rule between the User-agent lines you want to group. Some parsers tolerate blank lines there, but the format is clearer without them.
Three complete policies
Each policy below corresponds to a goal from the previous chapter. Pick one as a starting point and adjust the tokens.
Policy 1: opt out of training, stay visible in AI search
This blocks the tokens that control training use and leaves search and user-initiated fetching open:
# Opt out of model training, stay available for AI search and user requests
User-agent: GPTBot
Disallow: /
User-agent: Google-Extended
Disallow: /
User-agent: ClaudeBot
Disallow: /
User-agent: CCBot
Disallow: /
User-agent: Applebot-Extended
Disallow: /
User-agent: OAI-SearchBot
Allow: /
User-agent: ChatGPT-User
Allow: /
User-agent: *
Allow: /Blocking Google-Extended does not change how Googlebot crawls, indexes, or ranks your pages in Google Search. It is a control token that tells Google not to use your content for certain generative AI purposes. Google's crawling still happens under the Googlebot user agent.
Policy 2: block one section only
When you only want to protect certain directories, list those paths and leave the rest alone:
# Keep one section out of AI training, allow the rest
User-agent: GPTBot
Disallow: /members/
Disallow: /drafts/
User-agent: Google-Extended
Disallow: /members/
Disallow: /drafts/Because paths not matched by a Disallow rule are allowed, the rest of the site stays crawlable by these two crawlers. Crawlers with no group of their own fall through to the default of allowed.
Policy 3: block a section but keep one public page inside it
Use Allow to carve out an exception. The longer path wins:
User-agent: GPTBot
User-agent: Google-Extended
Allow: /members/public/
Disallow: /members/
Disallow: /drafts/
User-agent: *
Disallow: /drafts/Here the * group also hides /drafts/ from every other compliant crawler, and the training group repeats that rule because it replaces *.
Patterns and common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| `Disallow: members` (no leading slash) | Rule may not match anything | Start every path with `/` |
| `Disallow: /Members/` when the URL is `/members/` | No match, because paths are case-sensitive | Match the URL's actual case |
| `User-agent: GPT-Bot` or another misspelled token | The crawler never sees the rule | Copy the token from the operator's documentation |
| `Disallow: /private` when you mean the folder | Also blocks `/private-notes.html` and `/privateer` | Use `/private/` for a directory |
| Rules placed before any `User-agent` line | Rules are ignored | Start each group with `User-agent` |
| Putting `Crawl-delay` in the file for AI crawlers | Not part of RFC 9309, and many crawlers ignore it | Rate-limit on the server instead |
| One file for several subdomains | Only the host that serves it is covered | Serve a file on each host |
Also remember that robots.txt is itself public. Listing /secret-admin/ in a Disallow line tells everyone the path exists. Protect sensitive areas with authentication, not with robots rules.
Deploying and checking the file
After you publish the file, confirm that the live version is what you wrote. The next chapter covers testing in depth. As a first check, make sure the headers and body look right:
# Confirm the live file is served at the site root with a 200 status and plain text type
curl -sI https://example.com/robots.txt | head -n 5
# Read the rules exactly as crawlers will see them
curl -s https://example.com/robots.txtLook for HTTP/2 200 and a content-type: text/plain header. A CDN or CMS that returns an HTML page with a 200 status at /robots.txt can fool you. The file looks present but contains no valid rules.
Before you deploy, you can also test a draft locally with Python's standard-library parser. This script checks Policy 3 for several user agents and URLs:
from urllib.robotparser import RobotFileParser
rules = """\
User-agent: GPTBot
User-agent: Google-Extended
Allow: /members/public/
Disallow: /members/
Disallow: /drafts/
User-agent: *
Disallow: /drafts/
"""
parser = RobotFileParser()
parser.parse(rules.splitlines())
checks = [
("GPTBot", "https://example.com/articles/guide"),
("GPTBot", "https://example.com/members/account"),
("GPTBot", "https://example.com/members/public/faq"),
("OtherBot", "https://example.com/members/account"),
("OtherBot", "https://example.com/drafts/post"),
]
for agent, url in checks:
print(agent, url, parser.can_fetch(agent, url))
# GPTBot https://example.com/articles/guide True
# GPTBot https://example.com/members/account False
# GPTBot https://example.com/members/public/faq True
# OtherBot https://example.com/members/account True
# OtherBot https://example.com/drafts/post FalseChoosing between a blanket block and path rules
The policy you pick should follow from what you want to protect.
- Use
Disallow: /when you do not want the crawler's operator to use any of your content for that purpose. This is the simplest and least error-prone option. - Use path rules when only part of the site is sensitive, such as paid content, user-generated pages, or drafts.
- Keep search and user-initiated tokens separate from training tokens. OpenAI documents
GPTBot,OAI-SearchBot, andChatGPT-Useras separate user agents with separate purposes. Blocking one does not block the others. Fetchers that act on a user's request may not follow robots.txt in the same way as automated crawlers, so check each operator's documentation for its current behavior. - If you block a crawler, expect the change to take effect only after it next fetches your
robots.txt. Content collected before the block is generally not removed by the file.
Keeping the file maintainable
- Put a comment above each group saying why it exists and the date it was added.
- Keep one group per purpose: training, AI search, general default.
- Store the file in version control and deploy it with the rest of the site, so a bad edit can be reverted.
- Re-run your parser tests whenever you add or rename a token.
Exercise
Your file contains User-agent: * with Disallow: /drafts/, and a separate group User-agent: GPTBot with Disallow: /members/. Can GPTBot fetch /drafts/post-1? Explain, and show the corrected GPTBot group.
Show answer
Yes, GPTBot can fetch it. A crawler uses only the group that names it most specifically, so GPTBot follows its own group and ignores the * group. Its group does not disallow /drafts/, so the path is allowed by default. The corrected group is: User-agent: GPTBot, Disallow: /members/, Disallow: /drafts/.
Exercise
A group contains Disallow: /members/ and Allow: /members/public/. Which rule decides the fate of /members/public/faq, and why? What would you change to make the file behave the same in Python's robotparser?
Show answer
The Allow: /members/public/ rule decides it, so the URL is allowed. Under RFC 9309 the longest matching path wins. Python's parser uses the first matching rule in file order, so list the Allow line before the Disallow line to get the same result in both.
Exercise
You get a 404 when you request https://example.com/robots.txt after a deployment. What does this mean for the AI crawlers you blocked, and what should you check first?
Show answer
Compliant crawlers treat a 4xx response as no restrictions, so every crawler you blocked may now fetch the whole site. Check that the file was included in the deployment and is served from the site root of that exact host, then confirm a 200 status with curl -sI https://example.com/robots.txt.