Sub topic 5
Which group and which rule applies
This chapter assumes you can read a basic robots.txt file with User-agent, Disallow and Allow lines, and that you know the * and $ pattern characters. Here you will learn how a crawler picks one group of rules and then one rule inside that group.
Step 1: the crawler picks one group
A robots.txt file can hold several groups. A group is one or more User-agent lines followed by the rules beneath them. A crawler obeys only one group, the one whose User-agent value is the most specific match for its own name.
- Crawler names are matched case-insensitively.
Googlebotandgooglebotare the same. - A named group beats the
*group. If a group names your crawler, the*group is ignored for that crawler. - Rules are not merged across different groups. A crawler with its own group does not inherit anything from
*. - If no group matches, nothing is blocked.
User-agent: *
Disallow: /private/
User-agent: Googlebot
Disallow: /drafts/Here Googlebot may crawl /private/. Its group only blocks /drafts/. To block both folders for Googlebot, repeat /private/ in its group.
Step 2: the longest matching rule wins
Inside the chosen group, a crawler collects every Allow and Disallow rule whose path matches the URL. It then applies the rule with the longest path, counted in characters. Wildcards count as characters. Order in the file does not matter for modern crawlers such as Googlebot.
| Rules in the group | URL | Result | Reason |
|---|---|---|---|
| `Disallow: /shop/` and `Allow: /shop/sale/` | `/shop/sale/shoes` | Allowed | The `Allow` path is longer |
| `Disallow: /shop/` and `Allow: /shop/sale/` | `/shop/new/shoes` | Blocked | Only the `Disallow` matches |
| `Disallow: /page` and `Allow: /page` | `/page` | Allowed | Same length, so the less restrictive rule wins |
| `Disallow: /*.pdf$` | `/docs/a.pdf` | Blocked | The pattern matches |
| (no matching rule) | `/blog/` | Allowed | Crawling is allowed by default |
Paths are case-sensitive. Disallow: /Admin/ does not block /admin/.
Checking group selection in Python
Python's urllib.robotparser shows the group behavior. Its rule matching differs from Google's. It uses the first matching rule in file order, not the longest. If you test Allow exceptions with it, put the Allow line first.
from urllib.robotparser import RobotFileParser
rules = [
"User-agent: *",
"Disallow: /private/",
"",
"User-agent: Googlebot",
"Disallow: /drafts/",
]
rp = RobotFileParser()
rp.parse(rules)
base = "https://www.example.com"
print(rp.can_fetch("Googlebot", base + "/private/a.html")) # True
print(rp.can_fetch("Googlebot", base + "/drafts/a.html")) # False
print(rp.can_fetch("OtherBot", base + "/private/a.html")) # FalseCommon errors
- Adding a bot-specific group and expecting it to keep the
*rules. - Assuming file order decides conflicts. For Google, path length does.
- Writing rules before any
User-agentline. Crawlers ignore them. - Mixing up letter case in paths.
Exercise
A robots.txt has "User-agent: *" with "Disallow: /files/", and "User-agent: Bingbot" with "Allow: /files/public/". Can Bingbot fetch /files/internal/report.html? Can another crawler fetch /files/public/info.html?
Show answer
Yes to the first. Bingbot uses only its own group, which has no matching Disallow, so the URL is allowed by default. No to the second. The other crawler uses the * group, where "/files/" blocks the URL and no Allow overrides it.