Sub topic 3
robots.txt: what a crawler may fetch
This chapter covers the file a crawler reads before it requests anything else from a host. You should already know HTTP status codes and have read the previous chapter on fetching pages. By the end you should be able to write robots.txt rules, predict which rule a crawler applies, test them in Python, and explain why robots.txt does not control indexing.
What robots.txt is
robots.txt is a plain-text file served at the root of a host, for example https://example.com/robots.txt. It implements the Robots Exclusion Protocol, standardized as RFC 9309. A well-behaved crawler fetches it first, caches it, and checks every candidate URL against it before requesting the page.
The file is a request, not an enforcement mechanism. Reputable crawlers honor it. A malicious client can ignore it, and the file itself is public, so never list secret paths in it. Protect private content with authentication.
Each combination of protocol, host, and port has its own file. https://example.com/robots.txt does not cover http://example.com, https://blog.example.com, or https://example.com:8443.
Syntax: groups and rules
A file is made of groups. A group starts with one or more User-agent lines and is followed by rules. Blank lines separate groups.
User-agent: *
Disallow: /private/
Allow: /private/public-report.html
User-agent: ExampleBot
Disallow: /search
Sitemap: https://example.com/sitemap.xml| Directive | Meaning |
|---|---|
| `User-agent` | Names the crawler the group applies to. `*` matches any crawler without a more specific group. |
| `Disallow` | A path prefix the crawler should not fetch. An empty value blocks nothing. |
| `Allow` | A path prefix that is permitted, used to carve exceptions out of a `Disallow`. |
| `Sitemap` | The absolute URL of a sitemap. It is independent of groups. |
| `Crawl-delay` | A non-standard request for seconds between fetches. Some crawlers honor it. Google ignores it. |
Rules match from the start of the URL path, including the query string. Disallow: /search blocks /search, /search?q=cats, and /searches/old. Most major crawlers also support two pattern characters. * matches any run of characters, and $ anchors the end of the URL. For example, Disallow: /*.pdf$ blocks URLs ending in .pdf. Paths are case-sensitive.
Which group applies
A crawler obeys one group, the one whose User-agent token best matches its own name. It does not combine groups.
In the file above, ExampleBot follows only its own group. That group blocks /search and says nothing about /private/, so ExampleBot may fetch /private/data.html. Every other crawler uses the * group. If you want ExampleBot to respect the private rules too, repeat them in its group.
Agent matching ignores case and uses the product token, such as Googlebot, not the full User-Agent header string.
Which rule wins inside a group
When several rules match one URL, RFC 9309 and Google use the most specific rule, meaning the one with the longest matching path. If an Allow and a Disallow tie in length, Allow wins.
For /private/public-report.html, Disallow: /private/ is 9 characters and Allow: /private/public-report.html is 33. The Allow rule is longer, so the page is permitted.
Not every parser follows this. Python's urllib.robotparser applies the first matching rule in file order. With the file above, the Disallow appears first, so Python reports the report page as blocked.
Testing rules in Python
The standard library can fetch and query a live file. This is the usual way to check a URL before requesting it.
from urllib.robotparser import RobotFileParser
rp = RobotFileParser()
rp.set_url("https://example.com/robots.txt")
rp.read()
for url in ["https://example.com/", "https://example.com/private/data.html"]:
print(url, rp.can_fetch("ExampleBot", url))To test a draft file without a network request, pass its lines to parse(). This version also shows the first-match behavior. The Allow line is placed before the Disallow so that both Python and longest-match parsers agree.
from urllib.robotparser import RobotFileParser
rules = """\
User-agent: *
Allow: /private/public-report.html
Disallow: /private/
User-agent: ExampleBot
Disallow: /search
Sitemap: https://example.com/sitemap.xml
""".splitlines()
rp = RobotFileParser()
rp.parse(rules)
checks = [
("OtherBot", "https://example.com/private/data.html"),
("OtherBot", "https://example.com/private/public-report.html"),
("ExampleBot", "https://example.com/private/data.html"),
("ExampleBot", "https://example.com/search?q=cats"),
]
for agent, url in checks:
print(agent, url, rp.can_fetch(agent, url))It prints:
OtherBot https://example.com/private/data.html False
OtherBot https://example.com/private/public-report.html True
ExampleBot https://example.com/private/data.html True
ExampleBot https://example.com/search?q=cats FalseWhen the file cannot be read normally
The fetch of robots.txt is an HTTP request, and its status code changes what the crawler assumes.
- 2xx: The crawler parses the body and applies it.
- 3xx: The crawler follows redirects, usually up to a small limit such as five hops. Past that limit, it treats the file as unavailable.
- 4xx (such as 404): The file is treated as missing, so everything is allowed. Python's parser is stricter and treats 401 and 403 as "disallow everything."
- 5xx or a timeout: The server is in trouble. RFC 9309 says to assume everything is disallowed. Google stops crawling the site for a while and falls back to its last cached copy if it has one.
A robots.txt that returns a 500 error during an outage can therefore pause crawling of an entire site, even though no rule blocks anything. Crawlers also cache the file, commonly for up to 24 hours, so an edit may take a day to take effect. Parsers read only the first 500 KiB (the RFC minimum), so keep the file small.
What robots.txt does not do
robots.txt controls fetching. It does not control indexing, and the two are easy to confuse.
- A URL blocked by robots.txt can still appear in search results. If other pages link to it, the engine knows the URL exists and may list it with no description, because it never read the page.
- A
noindexdirective on a page cannot be seen if the crawler is forbidden to fetch that page. Blocking the page in robots.txt can therefore stopnoindexfrom working. - Blocking a page also prevents the crawler from reading its links, so link discovery through that page stops.
To keep a page out of the index, allow crawling and serve <meta name="robots" content="noindex, follow">. The chapter on indexing controls covers this in detail.
Common mistakes
- Blocking CSS and JavaScript. A renderer needs these files to build the page. Blocking them can make a page look empty or broken to the crawler.
- A stray
Disallow: /. This blocks the whole site. It often reaches production by copying a staging file. - Expecting group merging. Rules under
*do not apply to a crawler that has its own named group. - Missing trailing slash.
Disallow: /privatealso blocks/private-notes.html.Disallow: /private/blocks only the directory. - Wrong location. A file at
/blog/robots.txtis ignored. It must sit at the host root. - Relative sitemap URLs. The
Sitemapvalue must be a full absolute URL.
Exercise
A site's robots.txt contains "User-agent: *" with "Disallow: /drafts/", and a separate group "User-agent: ExampleBot" with "Disallow: /search". May ExampleBot fetch /drafts/post-1.html? The site owner also wants /drafts/post-1.html removed from search results, and adds "Disallow: /drafts/post-1.html" under every group. Why might this fail to remove it?
Show answer
Yes, ExampleBot may fetch it. A crawler obeys only its best-matching group, and the ExampleBot group does not mention /drafts/. For removal, the Disallow rule is the wrong tool. The crawler can no longer fetch the page, so it cannot see a noindex tag, and the URL can stay listed if other pages link to it. The owner should allow crawling and add a noindex directive to the page, then re-add the Disallow only after the page has dropped out of the index.
Exercise
Using Python's urllib.robotparser, the rules "Disallow: /docs/" followed by "Allow: /docs/faq.html" under "User-agent: *" report that /docs/faq.html is blocked. Why, and how do you fix the file?
Show answer
Python applies the first matching rule in file order, so the Disallow matches first. Move the Allow line above the Disallow line. Longest-match crawlers such as Google give the same answer either way, so the reordered file works for both.