robots.txt Rules Explained: User-agent, Disallow and Allow
Every robots.txt file is built from groups of lines. Each group names a crawler, then lists what it may and may not visit. Learn one group and you can read the whole file.
The first line: User-agent
A group begins with a User-agent line that specifies the target of the group.1
"User agent" is the name a crawler goes by. Googlebot is Google's main one.
Using an asterisk matches all crawlers, apart from the various AdsBot crawlers, which must be named directly.1 So this line means "everyone":
User-agent: *
Disallow: where not to go
A Disallow line names a directory or page that the crawler shouldn't visit. The path must start with a / character, and a directory should end with one too.1
User-agent: *
Disallow: /drafts/
This asks every crawler to stay out of the drafts folder.
Allow: exceptions
Allow does the reverse. It lets a crawler visit a page or folder inside a place you've otherwise blocked. Google describes it as a way to override a disallow rule for a subdirectory or page in a disallowed directory.1
User-agent: *
Disallow: /drafts/
Allow: /drafts/open-day.html
Here the drafts folder is off limits, except one page.
Which rule wins
When rules overlap, the standard says the most specific match is used.2 The most specific match is the one with the most characters in it. In the example above, the page rule is longer, so it wins for that one page.2
What happens with no rule
Google says the default assumption is that a user agent can crawl any page or directory not blocked by a Disallow rule.1 The standard agrees: if no match is found in a group for a matching user-agent, the address is allowed.2
So you only write rules for the places you want to ask crawlers to skip.
Small details that trip people up
- Rules are case-sensitive. A rule for /file.asp applies to that address but not to /FILE.asp.1
- A # character starts a comment, which crawlers ignore. Use comments to leave yourself notes.1
- Each rule gets its own line, with one rule per line.1
Naming a crawler or several
You can write a group for one crawler by its name, and another group for everyone else. A crawler can match only one group, the first and most specific one that fits it.1 Respectable crawlers follow the rules, but each crawler may read the syntax a little differently.3
Also remember the file asks. It doesn't enforce.
Where to go next
Not sure the file is in the right spot? The page on file location covers that. To see why a blocked page can still show up, read about blocked pages that appear anyway.
Cited material
- Create and Submit a robots.txt File, Google Search Central
- RFC 9309: Robots Exclusion Protocol, IETF
- Robots.txt Introduction and Guide, Google Search Central