Sub topic 5
Which group and which rule applies
This chapter assumes you can read a basic robots.txt file, know what User-agent, Disallow and Allow do, and can read short Python. When a file has several groups and overlapping rules, a crawler runs two separate selections. It first picks one group, then picks one rule inside that group. Mixing up these two steps causes most of the "my rule is ignored" bugs.
Step one: the crawler picks one group
A group is one or more User-agent lines followed by the rules beneath them. Groups are separated by blank lines, although blank lines are not strictly required.
User-agent: *
Disallow: /private/
User-agent: Googlebot
Disallow: /drafts/
User-agent: Googlebot-Image
Disallow: /photos/A crawler compares its own product token with every User-agent value and uses the most specific match. Matching ignores case, so googlebot and Googlebot are the same. If no named group matches, the crawler uses the * group. If there is no * group either, nothing is restricted.
The crawler then follows only the chosen group. Groups are not inherited or layered. In the example above:
- Googlebot fetches
/private/freely. Its own group does not mention/private/, and the*group is ignored for it. - Googlebot is blocked from
/drafts/. - Googlebot-Image uses its own group. It may fetch
/drafts/and/private/but not/photos/. - A crawler with no named group, such as an unknown bot, is blocked only from
/private/.
If you want a named bot to keep the general restrictions, copy those rules into its group.
Google also lets its specialized crawlers fall back to a more general Google group. For example, Googlebot-News follows the Googlebot group when it has no group of its own. Other search engines may handle fallback differently, so check each engine's documentation if you depend on it.
Repeated groups for the same agent
If the same user agent appears in two separate places, standard parsers merge those groups into one before matching.
User-agent: Googlebot
Disallow: /drafts/
User-agent: Bingbot
Disallow: /beta/
User-agent: Googlebot
Disallow: /tmp/Googlebot is blocked from both /drafts/ and /tmp/. Merging is defined in the robots.txt standard (RFC 9309) and Google implements it, but a simpler parser might use only the first group. Keep one group per agent so you never depend on this behavior.
Several User-agent lines can also share one rule set:
User-agent: Googlebot
User-agent: Bingbot
Disallow: /drafts/The Sitemap directive is not part of any group. It applies globally, wherever it appears in the file.
Step two: the most specific rule wins
Within the chosen group, a crawler collects every Allow and Disallow rule whose pattern matches the URL. It then keeps the rule with the longest pattern, counted in characters. Order in the file does not matter to Google. The rules to remember:
- Matching is done on the path plus the query string, starting at the leading
/. - Paths are case-sensitive.
Disallow: /Admin/does not block/admin/. - A URL that matches no rule is allowed. Crawling is permitted by default.
- The longest matching pattern wins. The
*and$characters count toward the length. - If an
Allowand aDisallowmatch with equal length,Allowwins, because the standard prefers the less restrictive rule. - An empty
Disallow:blocks nothing, and an emptyAllow:has no effect.
Here is a rule set and the outcome for some URLs:
| URL path | Matching rules | Winner | Result |
|---|---|---|---|
| `/admin/users` | `Disallow: /admin/` | that rule | Blocked |
| `/admin/help/faq` | `Disallow: /admin/` (7), `Allow: /admin/help/` (12) | Allow | Allowed |
| `/docs/manual.pdf` | `Disallow: /*.pdf$` (7), `Allow: /docs/` (6) | Disallow | Blocked |
| `/tmp` | `Disallow: /tmp` (4), `Allow: /tmp` (4) | Tie, Allow wins | Allowed |
| `/blog/` | none | no rule | Allowed |
The /docs/manual.pdf row surprises many people. /docs/ looks like the more specific rule by directory, but the PDF pattern is one character longer. To keep PDFs under /docs/ crawlable, write a longer allow rule such as Allow: /docs/*.pdf$.
Check the logic in code
Python's built-in urllib.robotparser is convenient but does not follow the longest-match rule. It applies the first rule in the file that matches, and it does not understand * or $ in paths. The same rules can therefore give different answers.
from urllib.robotparser import RobotFileParser
rp = RobotFileParser()
rp.parse([
"User-agent: *",
"Disallow: /admin/",
"Allow: /admin/help/",
])
print(rp.can_fetch("*", "https://www.example.com/admin/help/faq"))This prints False, because the Disallow line comes first. Google would allow the URL, because /admin/help/ is the longer match. Reversing the two lines makes Python print True. Do not treat this parser as a model of Google's behavior when you test files that use overlapping rules.
The following script implements the longest-match rule directly, including wildcards, end anchors, and the tie rule. It checks one group at a time, so group selection stays a separate step.
import re
def pattern_to_regex(pattern):
anchored = pattern.endswith("$")
if anchored:
pattern = pattern[:-1]
body = ".*".join(re.escape(part) for part in pattern.split("*"))
return re.compile("^" + body + ("$" if anchored else ""))
def is_allowed(rules, path):
best = None # (pattern length, is_allow)
for kind, pattern in rules:
if pattern == "":
continue
if pattern_to_regex(pattern).match(path):
candidate = (len(pattern), kind == "allow")
if best is None or candidate > best:
best = candidate
return True if best is None else best[1]
rules = [
("disallow", "/admin/"),
("allow", "/admin/help/"),
("disallow", "/*.pdf$"),
("allow", "/docs/"),
("disallow", "/tmp"),
("allow", "/tmp"),
]
for path in [
"/admin/users",
"/admin/help/faq",
"/docs/manual.pdf",
"/tmp",
"/blog/",
]:
print(path, is_allowed(rules, path))The output is:
/admin/users False
/admin/help/faq True
/docs/manual.pdf False
/tmp True
/blog/ TrueComparing the tuples (length, is_allow) does both jobs. A longer pattern always wins, and when lengths are equal True sorts above False, so Allow wins the tie.
Common mistakes
- Expecting the
*group to apply to a named bot. Once you add aUser-agent: Googlebotgroup, Googlebot stops reading the*group. Repeat shared rules in each group. - Relying on rule order. Google ignores order, but simpler parsers may not. Put the more specific
Allowbefore the broaderDisallowso both kinds of parser give the same result. - Forgetting case sensitivity.
/Photos/and/photos/are different paths. Add a rule for each spelling that exists on your site. - Forgetting that the query string is part of the match.
Disallow: /searchblocks/search?q=shoesbecause it is a prefix match.Disallow: /search$blocks only the bare/search. - Assuming
Disallow: /folderandDisallow: /folder/are equal. The first also blocks/folder-two/and/folder.html. The second blocks only paths inside the directory. - Writing unsupported directives. Parsers skip lines they do not recognize. Google ignores
Crawl-delay, and aNoindex:line in robots.txt has no effect.
Exercise
A robots.txt file contains the groups below. For Googlebot, is /blog/draft.html crawlable, and is /tmp/report.pdf crawlable? User-agent: Disallow: /blog/ Disallow: /tmp/ User-agent: Googlebot Disallow: /.pdf$ Allow: /tmp/
Show answer
Googlebot uses only its own group, so the rules do not apply to it. /blog/draft.html matches nothing in the Googlebot group, so it is crawlable. /tmp/report.pdf matches Disallow: /.pdf$ (7 characters) and Allow: /tmp/ (5 characters). The longer pattern wins, so the URL is blocked.
Exercise
Why can Python's urllib.robotparser say a URL is blocked when Google says it is allowed, even with identical rules?
Show answer
The Python parser applies the first matching rule in file order and does not support * or $ in paths. Google picks the longest matching pattern regardless of order. With Disallow: /admin/ listed before Allow: /admin/help/, Python blocks /admin/help/faq and Google allows it.