Dev.to WebDev 🛠 Dev 👁 0 📖 5 min read

robots.txt rules that hand-rolled parsers get wrong (tested against a small evaluator)

I build IndexCyborg, a tool that watches whether a website's pages get indexed. Before it tells anyone "Google hasn't indexed this page", it checks whether the site's own robots.txt blocks it. That meant writing a robots

I build IndexCyborg, a tool that watches whether a website's pages get indexed. Before it tells anyone "Google hasn't indexed this page", it checks whether the site's own robots.txt blocks it. That meant writing a robots.txt evaluator, and the format turned out to have more rules than its five keywords suggest.

Below are the ones that surprised us, each with a small example. Every verdict in this post comes from running the examples through our evaluator, about 100 lines of TypeScript written to follow Google's documented behaviour (which builds on RFC 9309).

1. A crawler obeys one group, not all of them

User-agent: *
Disallow: /private/

User-agent: Googlebot
Disallow: /drafts/

Can Googlebot fetch /private/report? Yes. Google picks the single group with the most specific user agent that matches the crawler. Here that's the Googlebot group, and the * group is ignored entirely for Googlebot. Bingbot, which has no group of its own, is blocked from /private/.

It's an easy mistake to make: you add a crawler-specific group "to add one rule" and silently drop every rule in * for that crawler. If you want both, repeat the shared rules in the specific group.

The selection in code:

function selectGroup(rules: RobotsRules, agent: string) {
  const agentLc = agent.toLowerCase();
  let best = "";
  for (const group of rules.groups) {
    for (const a of group.agents) {
      if (a !== "*" && agentLc.startsWith(a) && a.length > best.length) best = a;
    }
  }
  const token = best || "*";
  const groups = rules.groups.filter((g) => g.agents.includes(token));
  if (groups.length === 0) return null;
  return { agent: token, rules: groups.flatMap((g) => g.rules) };
}

Two details hide in there. A crawler with no group of its own falls back to a less specific one: with only a googlebot group, googlebot-image uses it. And the .filter(...).flatMap(...) at the end is the next rule.

2. Groups for the same crawler are merged

User-agent: googlebot
Disallow: /a/

User-agent: bingbot
User-agent: googlebot
Disallow: /b/

Googlebot is blocked from both /a/ and /b/. Google combines every group that names the selected agent into one. Bingbot is blocked only from /b/.

The second group also shows that consecutive User-agent lines share the rules that follow them. A parser has to remember whether the previous line was a User-agent line to know if a new one starts a new group:

if (field === "user-agent") {
  if (!lastWasAgent || !current) {
    current = { agents: [], rules: [] };
    groups.push(current);
  }
  current.agents.push(value.toLowerCase());
  lastWasAgent = true;
  continue;
}
lastWasAgent = false;

3. The longest match wins, not the first one

User-agent: *
Disallow: /shop/
Allow: /shop/sale/
Disallow: /shop/sale/old
Path Verdict Winning rule
/shop/boots blocked Disallow: /shop/
/shop/sale/new allowed Allow: /shop/sale/
/shop/sale/old-boots blocked Disallow: /shop/sale/old

Order in the file doesn't matter. The rule with the longest path that matches wins, so you can carve exceptions out of exceptions.

4. A tie goes to Allow

User-agent: *
Disallow: /page
Allow: /page

/page is allowed. When two matching rules are equally long, Google uses the least restrictive one. The evaluation loop:

let best: { rule: RobotsRule; len: number } | null = null;
for (const rule of group.rules) {
  const len = matchLength(pathname, rule.path);
  if (len >= 0 && (!best || len > best.len || (len === best.len && rule.allow && !best.rule.allow))) {
    best = { rule, len };
  }
}
return { allowed: best ? best.rule.allow : true, agent: group.agent, rule: best?.rule ?? null };

No matching rule means allowed. No applicable group at all also means allowed.

5. * and $, and the query string is part of the path

Rules match from the start of the path. * matches any run of characters, and $ anchors the end:

User-agent: *
Disallow: /*.pdf$
Disallow: /*?sessionid=
Path Verdict
/docs/a.pdf blocked
/docs/a.pdf?x=1 allowed: $ needs the path to end in .pdf
/docs/a.PDF allowed: paths are case-sensitive
/cart?sessionid=1 blocked
/cart?a=1&sessionid=2 allowed: the rule looks for ?sessionid=, not &sessionid=

That last row bites people who block tracking parameters: parameter order matters. You'd need a second rule for /*&sessionid=.

Our matcher turns the pattern into an anchored regular expression and uses the pattern's length as its specificity:

function matchLength(path: string, pattern: string): number {
  let regex = "";
  for (const ch of pattern) {
    if (ch === "*") regex += ".*";
    else if (ch === "$") regex += "$";
    else regex += ch.replace(/[.+?^${}()|[\]\\]/g, "\\$&");
  }
  try {
    return new RegExp(`^${regex}`).test(path) ? pattern.length : -1;
  } catch {
    return -1;
  }
}

Callers have to pass the path plus the query string (url.pathname + url.search), not just URL.pathname, or the second rule above can never match. One of our own callers, the background job that checks each page, passed pathname alone. Writing this post is how we found it.

6. Things that look like rules but aren't

Our tester flags these, because Googlebot ignores them without a word:

Disallow: /everything
User-agent: *
Crawl-delay: 10
Noindex: /old/
Disallow:
Sitemap: /sitemap.xml
  • Line 1: a rule before any User-agent line belongs to no group. /everything stays crawlable.
  • Crawl-delay isn't supported by Google (other crawlers do read it).
  • Noindex in robots.txt isn't supported by Google either. Use a noindex meta tag or an X-Robots-Tag header.
  • Disallow: with an empty value allows everything. We store it as Allow: /.
  • Sitemap must be a fully qualified URL with protocol and host. A relative path is an error. Sitemap lines also don't belong to any group: they apply no matter where they appear.

Field names are case-insensitive (user-agent, USER-AGENT); path values are not (/Admin/ doesn't block /admin/).

7. Size and status codes

  • Google reads the first 500 KiB of a robots.txt file and ignores the rest.
  • A 4xx answer (except 429) is treated as "no robots.txt": everything is allowed.
  • A 5xx answer is not. Google stops crawling the site for 12 hours while it retries, then uses the last cached copy for up to 30 days.

A confession: our checker treats any non-200 answer as "no rules", which matches Google for 4xx only. That's an acceptable shortcut for a checker that explains a page's status. It isn't acceptable for a crawler, which should back off on 5xx.

8. Blocked isn't the same as not indexed

The last surprise isn't about parsing. A page blocked by robots.txt can still show up in Google if other pages link to it: Google indexes the URL without its content. And a blocked page can't tell Google noindex, because Google never fetches it to read the tag.

So "allowed by robots.txt" is the first question to answer when a page isn't indexed, not the whole answer. If you want a page out of the index, allow crawling and serve noindex.

Checklist

  1. Check which group each crawler you care about actually uses. A specific group replaces *, it doesn't extend it.
  2. Expect merged groups and shared User-agent lines.
  3. Longest match wins; ties go to Allow.
  4. Match against path plus query, case-sensitively.
  5. Lint for rules Google ignores: Crawl-delay, Noindex, rules before the first User-agent, relative Sitemap URLs.
  6. Keep the file under 500 KiB, and make sure it never answers 5xx.

I wrote this with the help of an AI assistant, working from the project's commits and code. The snippets come from the repository, trimmed for length, and every verdict in the tables was produced by running that code.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.