Sub topic 1
How robots.txt works
Prerequisites
This chapter assumes you already know the following:
- How a crawler fetches pages over HTTP, and what the
User-Agentrequest header is. - How to edit files served from the root of a website.
- Basic
curluse on the command line. - Enough Python to read a short script.
You do not need to know anything about AI crawlers yet. Later chapters cover which crawlers exist and which ones to allow or block. This chapter covers the mechanism they all rely on.
What robots.txt is
robots.txt is a plain text file that tells automated clients which parts of a site they may fetch. The format is defined by the Robots Exclusion Protocol (REP), standardised as RFC 9309. The protocol started as an informal convention in the 1990s. Search engines followed it for decades before it became an IETF standard.
The protocol is built on three ideas:
- The file lives at one fixed location. A crawler looks for
/robots.txtat the root of each host. It does not search subdirectories. - Rules are addressed to crawlers by name. Each crawler announces a product token, such as
GPTBot. The file contains rules for that token. - Compliance is voluntary. The file states a request. Nothing in the protocol stops a client that ignores it. A later chapter covers this limit in detail.
This matters for AI crawlers because the whole control surface for them is this one file. Every decision you make about training data, AI search visibility, and user-initiated fetches comes down to which lines you write in it.
The request flow
Before a well-behaved crawler fetches a page, it fetches /robots.txt from the same host. It caches the result, usually for up to a day, and then decides whether the target URL is allowed.
Note that the check happens on the crawler's side. The server sends the same file to everyone and never sees the decision being made.
Where the file must live
The location rules cause many silent failures, so they are worth stating exactly.
- The URL must be
https://example.com/robots.txt. A file at/blog/robots.txtis ignored. - Each combination of protocol, host, and port has its own file.
https://example.com/robots.txtdoes not governhttps://blog.example.com/. Each subdomain needs its own file. - The file must be UTF-8 plain text and served with a
text/plaincontent type. - File size matters. RFC 9309 requires crawlers to parse at least 500 KiB. Content beyond a crawler's limit is ignored, so keep the file short.
The HTTP status code of the robots.txt response also changes what crawlers do:
| Response | What a compliant crawler does |
|---|---|
| 200 with content | Parses and obeys the rules |
| 404 or other 4xx | Treats the file as absent and crawls everything |
| 5xx or network error | Treats the whole site as disallowed for a period, then retries |
| Redirect | Follows a limited number of redirects, then treats the result as above |
The 5xx row is the trap. If a deploy error makes /robots.txt return 500, compliant crawlers may stop crawling your whole site, including search engines. A 4xx response has the opposite effect: the site becomes fully open. Neither is an error message you will see unless you look.
The structure of the file
A robots.txt file is a list of groups. Each group starts with one or more User-agent lines. Rule lines follow, and they apply to the crawlers named in that group.
User-agent: GPTBot
Disallow: /
User-agent: *
Allow: /This file has two groups:
- The first group names
GPTBotand disallows the whole site. - The second group uses
*, which matches every crawler that has no more specific group.
The main directives are:
| Directive | Meaning |
|---|---|
| `User-agent` | Names the crawler (product token) that the group applies to. `*` means any crawler not matched by another group. |
| `Disallow` | A path prefix the crawler should not fetch. An empty value disallows nothing. |
| `Allow` | A path prefix the crawler may fetch. Useful for making exceptions inside a disallowed path. |
| `Sitemap` | A full URL to a sitemap. It is not tied to any group. |
Other lines, such as Crawl-delay, exist in some implementations but are not part of the standard. Some crawlers honour them and others ignore them.
Other syntax rules:
- Directive names are case-insensitive. Path values are case-sensitive, so
/Members/and/members/are different paths. - A line starting with
#is a comment. - Blank lines separate groups by convention. Several
User-agentlines in a row share one rule set. - Each path is matched as a prefix against the URL path plus query string.
Disallow: /privateblocks/private,/private/, and/private-notes.html.
Choosing the right group
A crawler does not read every group. It selects exactly one group and ignores the rest.
- The crawler compares its product token with each
User-agentvalue. The match is case-insensitive. - If one or more groups name its token, it uses the group with the most specific match.
- If none match, it falls back to the
*group. - If there is no
*group either, there are no restrictions.
The key consequence is that groups do not combine. A crawler that finds its own group does not also read the * group. Consider this file:
User-agent: *
Disallow: /drafts/
User-agent: GPTBot
Disallow: /members/You might expect GPTBot to be blocked from both /drafts/ and /members/. It is not. GPTBot matches its own group, so it follows only Disallow: /members/ and can fetch /drafts/. If you want both rules to apply, repeat them in the GPTBot group.
When several User-agent lines share one group, all the named crawlers get the same rules:
User-agent: GPTBot
User-agent: CCBot
Disallow: /members/Matching Allow and Disallow
Inside the chosen group, a crawler checks the URL path against every Allow and Disallow rule. When several rules match, RFC 9309 says the longest matching path wins. If an Allow and a Disallow match with the same length, Allow wins.
User-agent: GPTBot
Disallow: /articles/
Allow: /articles/public/| URL path | Matching rules | Result |
|---|---|---|
| `/articles/private-note` | `Disallow: /articles/` | Blocked |
| `/articles/public/intro` | Both rules, and `/articles/public/` is longer | Allowed |
| `/about` | None | Allowed |
The last row shows the default. A path that no rule matches is allowed. So a Disallow list only blocks what it names, and you do not need to write Allow: / for the rest of the site.
Two wildcard characters are supported by major crawlers and by RFC 9309:
*matches any run of characters.Disallow: /*.pdfblocks any path containing.pdf.$anchors the end of the URL.Disallow: /*.pdf$blocks paths that end in.pdf.
Without $, rules are prefix matches, so Disallow: /*.pdf also blocks /report.pdf?download=1.
Common mistakes
These errors come up often enough to check for on every new file.
- Putting rules before any
User-agentline. Rules that are not inside a group are ignored. - Assuming
*is added to named groups. As shown above, a named group replaces the*group for that crawler. - Writing
Disallow:with nothing after it and expecting a block. An empty value allows everything. A full block needsDisallow: /. - Blocking a path in robots.txt to remove it from search results. robots.txt controls fetching. A blocked URL can still appear in search results if other pages link to it. Removing a page from an index needs a different tool, such as a
noindexrule on a page the crawler is allowed to fetch. - Forgetting subdomains.
www.example.comandexample.comare separate hosts with separate files. - Misspelling the token.
GPT-Botdoes not matchGPTBot. A misspelled group silently does nothing.
Checking a file quickly
You can confirm what the server is serving with curl. Check the status and headers first, then the body.
# 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.txtThe first command shows the status line and the Content-Type header. You want 200 and text/plain. The second prints the rules as a crawler receives them, including any changes made by a CDN or proxy that your local copy would not show.
Python's standard library includes a parser that follows the same group selection logic. This example shows the "named group replaces *" behaviour from earlier:
from urllib.robotparser import RobotFileParser
rules = """\
User-agent: *
Disallow: /drafts/
User-agent: GPTBot
Disallow: /members/
"""
parser = RobotFileParser()
parser.parse(rules.splitlines())
for agent in ["GPTBot", "SomeOtherBot"]:
for path in ["/drafts/post", "/members/area"]:
url = "https://example.com" + path
print(agent, path, parser.can_fetch(agent, url))
# GPTBot /drafts/post True
# GPTBot /members/area False
# SomeOtherBot /drafts/post False
# SomeOtherBot /members/area TrueWhat robots.txt does not do
It helps to be clear about the limits before you rely on the file.
- It does not authenticate anyone. Any client can read it, including people looking for the paths you listed. Do not put secret URLs in it.
- It does not enforce anything. A crawler that ignores it receives your pages normally.
- It does not remove content that was already collected. Rules apply to future fetches.
- It does not change what a page says. Controls that work at the page level, such as meta tags and HTTP headers, are separate mechanisms.
Later chapters build on this base. They show how the product tokens of AI crawlers map onto groups, and how to add server-side enforcement when a request is not enough.
Exercise
A site serves this robots.txt. Can GPTBot fetch /drafts/post-1? Can GPTBot fetch /members/account? Can a crawler named OtherBot fetch /members/account?
User-agent: *
Disallow: /drafts/
User-agent: GPTBot
Disallow: /members/
Show answer
GPTBot can fetch /drafts/post-1, because it matches its own group and ignores the * group, and that group blocks only /members/. GPTBot cannot fetch /members/account. OtherBot has no group of its own, so it uses *. It can fetch /members/account and cannot fetch /drafts/. To block GPTBot from both paths, add Disallow: /drafts/ to the GPTBot group.
Exercise
Your deploy breaks the application and https://example.com/robots.txt starts returning HTTP 503 for several hours. What may a compliant crawler do, and how is that different from the file returning 404?
Show answer
With a 5xx response, a compliant crawler may assume it is not allowed to crawl anything and pause crawling the whole site until it can read the file again. With a 404, the crawler treats the file as absent and assumes everything is allowed. A server error can therefore stop crawling, and a missing file opens the site fully. Monitor the status of /robots.txt and check it after each deploy.
Exercise
Given this group, which of these paths are allowed for GPTBot: /articles/public/intro, /articles/draft, /contact?
User-agent: GPTBot
Disallow: /articles/
Allow: /articles/public/
Show answer
/articles/public/intro is allowed, because both rules match and the longer Allow: /articles/public/ wins. /articles/draft is blocked by Disallow: /articles/, which is the only rule that matches. /contact is allowed, because no rule matches and the default is to allow.