Sub topic 7
Groups, Allow, Disallow, and how rules are matched
This chapter assumes you know what a User-agent header is and can edit the robots.txt file at your site root. It also assumes you can read short Python scripts. By the end you should be able to predict which rules a crawler applies to a URL, without trial and error.
The earlier chapters named the crawlers. Here you learn the grammar and the matching algorithm that decides what each one may fetch. Most real-world robots.txt mistakes come from misreading this algorithm, not from using the wrong crawler name.
The structure of a robots.txt file
A robots.txt file is a list of groups. A group starts with one or more User-agent lines. They are followed by rules, which are Allow and Disallow lines. Each rule has a path prefix.
User-agent: GPTBot
User-agent: ClaudeBot
Disallow: /members/
Allow: /members/public/
User-agent: *
Disallow: /admin/
Sitemap: https://example.com/sitemap.xmlThis file has two groups. The first applies to two crawlers at once, because consecutive User-agent lines share the rules that follow them. A new group starts when a User-agent line appears after at least one rule line. A blank line between groups is a convention for readability and is not required. The Sitemap line belongs to the whole file and does not belong to any group.
Some other details of the format:
- Directive names are case-insensitive. User-agent tokens are also case-insensitive, so
GPTBotandgptbotare the same. - Paths are case-sensitive.
/Members/and/members/are different prefixes. - Lines starting with
#are comments. A#also ends the value on a line. - Lines the parser does not recognize are ignored. A typo such as
Disalow:silently produces no rule. - The file applies only to the origin that serves it: the same protocol, host, and port.
https://example.com/robots.txtsays nothing abouthttps://blog.example.com/.
Step 1: choosing the group
A crawler reads the whole file and picks the group that matches its own user-agent token. If its token appears in more than one group, the rules from those groups are combined. The most specific match wins. If no group names the crawler, it uses the * group. If there is no * group either, nothing is restricted.
The key consequence is that a crawler obeys only one group (after merging groups with the same token). It does not also apply the * group.
You can confirm this with Python's standard-library parser.
from urllib.robotparser import RobotFileParser
rules = """\
User-agent: *
Disallow: /private/
User-agent: GPTBot
Disallow: /drafts/
"""
parser = RobotFileParser()
parser.parse(rules.splitlines())
for agent in ["GPTBot", "SomeOtherBot"]:
for path in ["/private/report", "/drafts/post"]:
url = "https://example.com" + path
print(agent, path, parser.can_fetch(agent, url))
# GPTBot /private/report True
# GPTBot /drafts/post False
# SomeOtherBot /private/report False
# SomeOtherBot /drafts/post TrueGPTBot is allowed into /private/ because its own group never mentions that path. The fix is to repeat the rule:
User-agent: GPTBot
Disallow: /private/
Disallow: /drafts/Step 2: choosing the rule
Within the chosen group, the crawler finds every rule whose path is a prefix of the URL path. If several match, the rule with the longest path wins. If an Allow and a Disallow tie on length, Allow wins, because the standard (RFC 9309) favors the least restrictive outcome. If no rule matches, the URL is allowed. The order of lines in the file does not matter for crawlers that follow the standard.
Take this group:
User-agent: GPTBot
Disallow: /articles/
Allow: /articles/public/A request for /articles/public/intro matches both rules. Allow: /articles/public/ has the longer path, so the URL is allowed. A request for /articles/internal/notes matches only the Disallow, so it is blocked. A request for /about matches neither, so it is allowed.
What counts as a match
Paths are prefix matches against the URL path plus its query string. A few special characters extend this:
*matches any sequence of characters.$at the end of a pattern anchors it to the end of the URL.
| Rule | Matches | Does not match |
|---|---|---|
| `Disallow: /fish` | `/fish`, `/fish.html`, `/fish/salmon`, `/fishheads` | `/Fish`, `/catfish` |
| `Disallow: /fish/` | `/fish/`, `/fish/salmon` | `/fish`, `/fish.html` |
| `Disallow: /*.pdf$` | `/docs/a.pdf` | `/docs/a.pdf?download=1` |
| `Disallow: /*?session=` | `/page?session=abc` | `/page?id=3` |
| `Disallow:` (empty) | nothing, so everything is allowed | nothing |
| `Disallow: /` | every URL on the host | nothing |
Two of these trip people up. A rule without a trailing slash is a plain text prefix, so /fish also catches /fishheads. An empty Disallow: means "nothing is disallowed", the opposite of Disallow: /. A single missing slash separates "allow everything" from "block everything".
When wildcards are involved, "longest" is measured by the length of the rule's pattern, wildcards included. Take Allow: /*.html$ (10 characters) and Disallow: /private/ (9 characters). A URL that matches both, such as /private/page.html, is allowed, because the Allow pattern is one character longer. Patterns like this are easy to get wrong, so always test them.
Matching crawler tokens
The token in User-agent is the product name the crawler announces, not the full User-Agent header string. A crawler matches case-insensitively and uses the group with the most specific token. For example, a crawler that has its own token uses that group in preference to a more general related one. If no such group exists, it falls back to the broader group it belongs to, and then to *.
Some tokens control behavior rather than name a crawler that visits your site. Google-Extended is the example from earlier chapters. Google's regular crawlers fetch pages, and the Google-Extended group tells Google whether the content may be used for certain AI purposes. The matching algorithm is the same either way. You write the group, and the operator reads it.
Fetch results change the meaning of the file
Crawlers do not always get a clean file. The standard describes what to do for each outcome of fetching /robots.txt:
| Fetch result | What a standards-following crawler does |
|---|---|
| 200 with content | Applies the rules |
| 4xx, such as 404 | Treats the file as missing, so everything is allowed |
| 5xx or unreachable | Treats the site as fully disallowed, at least temporarily |
| Redirects | Follows several hops, then treats the file as missing if it never resolves |
A broken server or a misconfigured redirect can therefore block everything or allow everything. Crawlers also read only a limited part of the file. The standard requires at least 500 KiB, and rules beyond the limit are ignored. Keep the file small and put the most important groups first.
A tester's caveat
The Python parser used above is useful but is not a reference implementation of RFC 9309. It applies rules in file order and uses the first one that matches, and it treats paths as plain prefixes without * or $ handling. For the group in the previous example, order matters in Python:
from urllib.robotparser import RobotFileParser
def check(rules, agent, url):
parser = RobotFileParser()
parser.parse(rules.splitlines())
return parser.can_fetch(agent, url)
allow_first = """\
User-agent: GPTBot
Allow: /articles/public/
Disallow: /articles/
"""
disallow_first = """\
User-agent: GPTBot
Disallow: /articles/
Allow: /articles/public/
"""
url = "https://example.com/articles/public/intro"
print(check(allow_first, "GPTBot", url)) # True
print(check(disallow_first, "GPTBot", url)) # FalseA standards-following crawler would allow the URL in both files, because the longest match wins. Python gives a different answer for the second file. When you write rules that mix Allow and Disallow, put the more specific Allow first. That works the same in both models. For anything using wildcards, use a parser that implements the standard, or check the operator's own testing tool.
Habits that prevent mistakes
- Put each crawler you care about in an explicit group, and repeat shared rules in each one.
- Prefer trailing slashes on directory rules, so
/members/does not also catch/membership-info. - Keep rules short and few. Every extra pattern adds a chance of an unintended longest-match result.
- Remember that the file only expresses a request. The next chapters cover how well crawlers honor it and how to enforce it.
Exercise
A robots.txt contains a * group with Disallow: /private/ and a separate ClaudeBot group with only Disallow: /drafts/. May ClaudeBot fetch /private/report.html? What single change blocks it?
Show answer
Yes, it may. ClaudeBot has its own group, so the * group does not apply to it, and nothing in its group matches /private/. Adding Disallow: /private/ to the ClaudeBot group blocks it.
Exercise
A group for GPTBot contains Disallow: /docs/ and Allow: /docs/guide in either order. Which of /docs/guide, /docs/guide/intro, /docs/reference and /docs/guidelines can a standards-following crawler fetch?
Show answer
/docs/guide, /docs/guide/intro and /docs/guidelines can be fetched, because Allow: /docs/guide is a prefix of each and is longer than Disallow: /docs/. /docs/reference matches only the Disallow and is blocked.
Exercise
What is the difference between Disallow: and Disallow: /, and what does a crawler do when the server returns a 503 for robots.txt?
Show answer
An empty Disallow: blocks nothing, so everything is allowed. Disallow: / blocks the whole site. On a 5xx response, a standards-following crawler treats the site as fully disallowed for the time being, so a server error can block crawling just as a rule would.