How robots.txt is actually parsed: groups, precedence and common mistakes
A robots.txt file looks simple enough that most people write one by intuition. The parsing rules do not match that intuition in a few important places, and the file gives no feedback when you get it wrong. This post walks through how a crawler reads the file, in the order it makes its decisions.
Where the file lives, and what it is for
A crawler looks for the file in exactly one place: the root of the origin, such as https://example.com/robots.txt. There is one file per scheme, host and port, which means:
- A file at
/blog/robots.txtis ignored. http://example.comandhttps://example.comare separate origins with separate files.shop.example.comneeds its own file. The one onexample.comdoes not cover it.
It also helps to be clear about what the file is. It is a crawling directive. It is not access control and it is not an indexing directive. A well behaved crawler chooses to obey it, and nothing stops anyone else from requesting the URL. Anything private needs authentication.
The indexing point catches people regularly. A blocked URL can still appear in search results if other pages link to it, because the crawler never fetches the page and so never sees a noindex on it. If you want a page out of the index, let it be crawled and serve noindex.
Step one: the crawler picks one group
The file is made of groups. Each group is one or more User-agent lines followed by Allow and Disallow rules.
User-agent: *
Disallow: /admin/
Disallow: /cart/
User-agent: GPTBot
Disallow: /drafts/
A crawler obeys exactly one group: the one whose User-agent value matches its own token most specifically. User-agent: * is the fallback, used only when no specific group matches.
The consequence is the single most common mistake in these files. A specific group does not inherit anything from the wildcard group. In the example above, GPTBot is blocked from /drafts/ and from nothing else. It may crawl /admin/ and /cart/, because once it found its own group it stopped reading the * group entirely.
If you want the shared rules to apply, repeat them:
User-agent: GPTBot
Disallow: /admin/
Disallow: /cart/
Disallow: /drafts/
Alternatively, stack several User-agent lines above one set of rules so that they share a group. The token match ignores case, so gptbot and GPTBot select the same group.
Step two: the most specific rule wins
Within the chosen group, order does not matter. The crawler does not take the first matching rule or the last one. It takes the most specific one, which means the rule with the longest matching path.
User-agent: *
Disallow: /shop/
Allow: /shop/public/
For /shop/public/sale.html, both rules match. Allow: /shop/public/ is longer, so the page may be crawled. For /shop/basket, only the Disallow matches, so it is blocked. Swapping the two lines changes nothing.
Two more details complete the picture:
- When an
Allowand aDisallowmatch at the same length,Allowwins. - An empty
Disallow:value allows everything.Disallow: /blocks everything. The difference is one character.
How paths are matched
Rules are compared against the path and query string, starting from the first /. Matching is case sensitive and prefix based.
Prefix based means a rule matches any URL that starts with it. Disallow: /admin blocks /admin/, but it also blocks /administrator and /admin-guide.html. If you mean the directory, write Disallow: /admin/ with the trailing slash.
Case sensitive means Disallow: /Private/ does nothing for /private/. Only the paths behave this way. Directive names such as Disallow are not case sensitive.
Two special characters extend the prefix match:
| Pattern | Matches | Does not match |
|---|---|---|
/admin | /admin, /admin/users, /administrator | /Admin |
/admin/ | /admin/, /admin/users | /admin, /administrator |
/*.pdf$ | /files/report.pdf | /files/report.pdf?v=2 |
/*?sort= | /shoes?sort=price | /shoes |
* stands for any sequence of characters, and $ anchors the end of the URL. Without the $, /*.pdf would also match /files/report.pdf?v=2, because everything remains a prefix match by default. Paths with special characters are compared in their percent encoded form, which URL encoding explained covers.
The other directives
Sitemap: is separate from the group structure. It applies regardless of which user agent group it appears near, so it can go anywhere in the file, and it takes a full absolute URL including the scheme. A relative path is not valid here. See absolute vs relative URLs for the distinction.
Sitemap: https://example.com/sitemap.xml
Crawl-delay is not supported by every crawler. Some honour it and some ignore it completely, so do not rely on it as your only protection against load.
When the file itself fails to load
How the server answers the request for robots.txt matters as much as what the file says.
- 404 or 410: there is no file, so everything is allowed.
- Persistent 5xx: treated as everything disallowed. The crawler cannot tell what you intended, so it assumes the cautious answer and stays away.
- Redirect: followed, within reason.
The 5xx case is the dangerous one. A misconfigured proxy, a firewall rule that challenges bots, or an application error on that one route can stop crawling of the whole site while every page a human visits works normally. If crawling drops for no visible reason, check the status of /robots.txt before anything else.
Mistakes that block the wrong thing
Assuming inheritance. Adding a short group for one bot and expecting the * rules to still apply to it.
Blocking CSS and JavaScript. A crawler that renders pages needs the assets. Blocking /static/ or /assets/ means it sees an unstyled or empty page, and that distorts how the page is understood.
Using it to hide things. The file is public. Listing /secret-reports/ in it tells everyone where to look.
Treating all bots as one decision. Blocking AI crawlers is a separate choice from blocking search crawlers, and each uses its own user agent token. A rule for GPTBot has no effect on Googlebot, and the reverse is also true. Which AI crawlers can read your site goes through the options.
Because of the group rule, the only reliable way to know what a given bot may do is to evaluate the file as that bot. The AI Crawler Checker fetches your robots.txt and reports which AI crawlers are allowed and which are blocked, which is quicker than tracing groups by hand.