Sub topic 3
Writing User-agent, Disallow, and Allow rules
This chapter covers the rule syntax inside a robots.txt file. It assumes you know that robots.txt sits at the root of a host and that it controls crawling, not indexing. It also assumes you can read a URL path and a query string. Wildcards (* and $) and the full precedence logic have their own chapters. Here you learn what each line means and how to avoid the usual mistakes.
The anatomy of a rule file
A robots.txt file is a list of groups. Each group says which crawler it addresses and which paths that crawler may or may not request. A group has two parts:
- One or more
User-agentlines naming the crawlers. - One or more
DisalloworAllowlines listing path rules.
# Rules for every crawler
User-agent: *
Disallow: /admin/
Disallow: /search
# Extra rules for one crawler
User-agent: Googlebot-Image
Disallow: /photos/private/
Sitemap: https://www.example.com/sitemap_index.xmlEach directive goes on its own line in the form Name: value. A # starts a comment that runs to the end of the line. Blank lines separate groups visually, but a blank line is not what ends a group. A new User-agent line after rule lines starts the next group.
Syntax rules that trip people up
- Directive names (
User-agent,Disallow,Allow) are not case-sensitive. Crawlers readdisallow:the same way. - Path values are case-sensitive.
Disallow: /Admin/does not block/admin/. - Crawler names in
User-agentlines are matched without regard to case. - Paths start with
/. A value likeDisallow: admin/is malformed, and crawlers may ignore it. - Lines a crawler does not recognize are skipped. A typo such as
Dissallow:fails silently, and nothing is blocked. - The file should be UTF-8 plain text. A word processor that adds formatting breaks it.
User-agent: who the group is for
User-agent names the crawler a group applies to. Each crawler publishes a product token, such as Googlebot or Bingbot, which you put in this line. The token * means every crawler that has no more specific group of its own.
User-agent: Googlebot
User-agent: Bingbot
Disallow: /beta/Several User-agent lines in a row share the rules that follow them. In this example, both Googlebot and Bingbot are kept out of /beta/.
Be careful with the idea that * applies to everyone. A crawler that finds a group naming it exactly uses that group and ignores the * group completely. Rules are not merged. If you write a Googlebot group, you must repeat any * rules that Googlebot should still follow. The chapter on group selection covers this in detail.
Some Google crawlers, such as AdsBot, do not obey the * group at all. They must be named explicitly. Check each search engine's documentation for the tokens you care about.
Disallow: what a crawler should not fetch
Disallow gives a path prefix. A URL is covered if its path (including the query string) begins with that value.
| Rule | Blocks | Does not block |
|---|---|---|
| `Disallow: /admin/` | `/admin/`, `/admin/users` | `/admin` (no trailing slash), `/administrator/` |
| `Disallow: /admin` | `/admin`, `/admin/users`, `/administrator/`, `/admin.html` | `/blog/admin` |
| `Disallow: /search` | `/search`, `/search?q=shoes`, `/search-tips` | `/en/search` |
| `Disallow: /` | Every URL on the host | Nothing |
| `Disallow:` (empty) | Nothing | Every URL |
Three points follow from the table.
- Matching is by prefix.
/adminis a prefix of/administrator/, so it blocks that too. Add the trailing slash when you mean a directory. - The path is matched from the start.
Disallow: /searchdoes not catch/en/search. Each language folder needs its own rule, or a wildcard (next chapter). - An empty
Disallow:allows everything. It is the standard way to say "this crawler has no restrictions". A single stray/flips the meaning to "block the whole site".
<br>
Allow: carving out exceptions
Allow re-opens a path inside an area that a broader Disallow closed. It is mostly useful for exceptions.
User-agent: *
Disallow: /admin/
Allow: /admin/help/Here everything under /admin/ is blocked except /admin/help/ and what lies beneath it. Without any Disallow, an Allow line has no effect, because everything is allowed by default. You do not need Allow: / on its own.
When Disallow and Allow both match a URL, the crawler must pick one. Google and most modern crawlers choose the rule with the longer, more specific path. Here /admin/help/ is longer than /admin/, so the Allow wins for URLs under it. Some older parsers instead use the first matching line in file order. If you want rules to work in both kinds of parser, put the more specific Allow before the broader Disallow:
User-agent: *
Allow: /admin/help/
Disallow: /admin/Google gives the same result for both orderings, and first-match parsers now behave correctly too.
The Sitemap directive
Sitemap is different from the other directives. It is not part of any group, so it can appear anywhere in the file and applies regardless of which User-agent lines are near it. The value must be a full absolute URL, including the scheme.
Sitemap: https://www.example.com/sitemap_index.xml
Sitemap: https://www.example.com/news-sitemap.xmlYou can list several sitemaps, one per line. A relative value like Sitemap: /sitemap.xml is a common error. Sitemap discovery gets its own chapter later.
A complete example, line by line
This file blocks an admin area, internal search, session-ID URLs, and PDFs for all crawlers. It re-allows one subfolder and points to the sitemap index.
User-agent: *
Disallow: /admin/
Disallow: /search
Disallow: /*?sessionid=
Disallow: /*.pdf$
Allow: /admin/help/
Sitemap: https://www.example.com/sitemap_index.xmlThe first, second, and fifth rules are plain prefixes, which you now know how to read. The third and fourth use * and $, which the next chapter explains. For now, read them as "any URL containing ?sessionid=" and "any URL ending in .pdf".
Checking rules with code
Python's standard library includes a robots.txt parser. It is handy for quick checks of simple rules.
from urllib.robotparser import RobotFileParser
rules = [
"User-agent: *",
"Disallow: /private/",
]
rp = RobotFileParser()
rp.parse(rules)
print(rp.can_fetch("*", "https://www.example.com/private/report.html"))
print(rp.can_fetch("*", "https://www.example.com/blog/"))The first call prints False and the second prints True.
Treat this parser as a rough check, not as proof of how a search engine will behave. It uses a simple matching model, applies rules in file order, and does not support all of the pattern syntax that Google does. Test final rules with the tools the search engine provides.
Other lines you may see
Crawl-delayasks a crawler to wait a number of seconds between requests. Bing honors it. Google ignores it.Noindexinside robots.txt is not supported by Google and should not be relied on. Use a meta robots tag or anX-Robots-Tagheader instead. The chapter on noindex versus disallow explains why.Hostand other vendor-specific lines are ignored by most crawlers.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| `Disallow: /` left over from staging | Whole site blocked | Remove it or use `Disallow:` |
| `Disallow: admin/` (no leading slash) | Rule may be ignored | Write `Disallow: /admin/` |
| Wrong case, `/Admin/` instead of `/admin/` | Nothing is blocked | Match the real URL case |
| Multiple paths on one line | Only malformed rule | One path per `Disallow` line |
| Rules placed before any `User-agent` line | Rules belong to no group and are ignored | Start each group with `User-agent` |
| Relative Sitemap URL | Sitemap not found | Use a full `https://` URL |
| Extra named group omits `*` rules | Named crawler loses the general rules | Repeat shared rules in the named group |
Exercise
A site has this robots.txt. For the URLs /tools/, /tools/calc, /toolbox/, and /Tools/, say whether a crawler following the group may fetch each one.
User-agent: *
Disallow: /tools/
Allow: /tools/calc
Show answer
/tools/ is blocked, because it begins with /tools/ and no Allow rule matches it. /tools/calc is allowed, because the Allow rule is longer and more specific than the Disallow rule. /toolbox/ is allowed, because it does not begin with /tools/. /Tools/ is allowed, because paths are case-sensitive and the capital T makes it a different path.
Exercise
You want Bingbot to have no restrictions, and every other crawler to stay out of /drafts/. Write the robots.txt.
Show answer
User-agent: Bingbot
Disallow:
User-agent:
Disallow: /drafts/
Bingbot finds its own group and uses it, so the empty Disallow: allows everything for it. All other crawlers fall back to the ` group and are kept out of /drafts/`.