ToolsWhisper

robots.txt Allow vs Disallow: Which Rule Wins?

The order of lines does not decide it. The longest matching rule wins, a tie goes to Allow, and the group is chosen first. Worked examples you can test.

Frank4 min read

If your robots.txt has both an Allow and a Disallow that match a URL, which one applies? It is not the first one, and it is not the last one. The answer is a short rule, and once you have it you can read any robots.txt correctly.

The rule comes from the Robots Exclusion Protocol, standardised in 2022 as RFC 9309 and implemented by Google's crawler and others.

Step 1: the crawler picks one group

A robots.txt is a list of groups. Each starts with one or more User-agent lines. A crawler first finds the group whose user-agent matches its own name, ignoring case, and falls back to the * group if there is none.

Two consequences catch people out:

  • A specific group replaces *; it does not add to it. If you have a * group that disallows /private/ and a separate Googlebot group that does not mention it, Googlebot may crawl /private/. It follows only its own group.
  • Groups for the same bot are combined. Two separate Googlebot groups are read as one.

Step 2: the longest match wins

Within that group, every Allow and Disallow whose pattern matches the URL's path (and query) is a candidate. The most specific one is used, and specific means longest: the rule that matches the most characters.

If an Allow and a Disallow match with the same length, Allow wins.

The order of lines does not matter. That surprises people who know firewall rules, where the first match wins.

Worked examples

Take this file:

User-agent: *
Disallow: /shop/
Allow: /shop/sale/
Disallow: /*.pdf$
Allow: /downloads/*.pdf$

And test some URLs against it, as Googlebot:

URL Result Decided by
/shop/shoes Blocked Disallow: /shop/
/shop/sale/shoes Allowed Allow: /shop/sale/ is longer
/files/a.pdf Blocked Disallow: /*.pdf$
/downloads/guide.pdf Allowed Allow: /downloads/*.pdf$ is longer
/downloads/guide.pdf?x=1 Allowed No rule matches, so it is allowed

That last row is the one to study. The patterns that end in $ match only when the URL ends there. Add ?x=1 and neither PDF rule matches, so the default applies, which is allowed. If your intention was to block every PDF, a query string defeats /*.pdf$. Drop the $ to match .pdf anywhere in the path.

A tie goes to Allow

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

Both rules are the same length, so /page is allowed. This is the one place order-independence can feel wrong: writing a Disallow after an Allow does not override it.

The patterns

Only two special characters exist:

  • * matches any sequence of characters, including none.
  • $ at the end anchors the match to the end of the URL.

Everything else is a literal prefix, so /shop blocks /shop, /shop/, /shopping and /shop-sale. Add the trailing slash, /shop/, to limit it to the directory. Matching is case-sensitive: /Admin is not /admin.

An empty Disallow

Disallow: with nothing after it disallows nothing, so a group that contains only that allows everything. It is the conventional way to say "no restrictions". It is not the same as Disallow: /, which blocks the whole site.

Where the file has to live

A robots.txt applies only to the host, protocol and port it is served from. https://example.com/robots.txt covers https://example.com/ and nothing else: not https://blog.example.com/, and not http://example.com/. Each of those needs its own file. It must be named exactly robots.txt and sit at the root.

When the file is missing or broken

RFC 9309 says what a crawler should assume when it cannot read the file properly:

  • A 404 or other client error means there are no restrictions, and the whole site may be crawled.
  • A server error (5xx) or an unreachable server means the crawler must assume everything is disallowed for the time being.

So a missing file never blocks anything, but a robots.txt that is erroring can pause crawling of your entire site. Crawlers may also keep a copy for up to 24 hours, which is why a change you make does not take effect instantly.

A size limit

Crawlers must read at least the first 500 kibibytes of the file, and may ignore anything after that. A hand-written file never gets near it, but a generated one listing thousands of URLs can. If yours is large, put the important rules first.

A checklist for a URL that is not behaving

  1. Is the file served from the exact host and protocol you are testing?
  2. Does it return a 200 right now?
  3. Which group matches your crawler's name? Remember that a specific group replaces *.
  4. Which matching rule is the longest?
  5. Do a $ or a query string change the match?
  6. Does the case of the path match? /Admin and /admin are different.

Test it, do not guess

The cost of misreading these rules is either a section of your site disappearing from search or a private one being crawled. Paste your file into the robots.txt tester, list the URLs you care about, and it shows for each one whether it is allowed and exactly which line decided it. That is the fastest way to check a change before it goes live.

If you are blocking AI training bots as well, the same group rules apply: see how to block AI crawlers in robots.txt. And for what robots.txt cannot do at all, such as removing a page from search, read the earlier overview.

Tools used in this article

Read next