Sub topic 3
Writing User-agent, Disallow, and Allow rules
This chapter assumes you know what a URL path is, how HTTP status codes work, and the difference between crawling and indexing. It covers the three core robots.txt directives and how they combine into groups.
Anatomy of a group
A robots.txt file is a list of groups. Each group starts with one or more User-agent lines. Those lines are followed by Disallow and Allow rules that apply to the named crawlers.
User-agentnames the crawler the group applies to.*means every crawler that has no more specific group.Disallowgives a path prefix the crawler should not request.Allowgives a path prefix the crawler may request, which is used to carve out exceptions.
Paths are matched from the start of the URL path, and they are case-sensitive. Disallow: /admin/ blocks /admin/users but not /Admin/users. An empty Disallow: value blocks nothing. Disallow: / blocks the whole site.
A worked example
User-agent: *
Disallow: /admin/
Disallow: /search
Disallow: /*?sessionid=
Disallow: /*.pdf$
Allow: /admin/help/
Sitemap: https://www.example.com/sitemap_index.xmlReading it line by line:
User-agent: *opens a group for all crawlers.Disallow: /admin/blocks everything under the admin folder.Disallow: /searchblocks internal search results. Because matching is by prefix, it also blocks/search?q=shoesand/search/results.Disallow: /*?sessionid=blocks URLs that carry a session ID. The*and$characters are covered in the next chapter.Disallow: /*.pdf$blocks URLs that end in.pdf.Allow: /admin/help/re-opens one subfolder inside the blocked area.Sitemap:points to the sitemap index. It is not part of any group, and you can place it anywhere in the file.
Naming specific crawlers
You can write a group for one crawler. Crawlers use the group with the most specific matching name and ignore the * group.
User-agent: *
Disallow: /drafts/
User-agent: Googlebot-Image
Disallow: /photos/Here Googlebot-Image follows only its own group. It may crawl /drafts/, because the * rules do not apply to it. If you want it to avoid /drafts/ as well, repeat that rule in its group.
Several User-agent lines can share one set of rules:
User-agent: Googlebot
User-agent: Bingbot
Disallow: /beta/Checking rules in code
Python's standard library can parse simple rules and answer allow or deny questions.
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.
Common mistakes
- Putting a rule before any
User-agentline. Such a rule belongs to no group and is ignored. - Forgetting the leading slash. Write
/admin/, notadmin/. - Assuming
Disallowremoves a page from search results. It only stops crawling. The next chapters explain the difference. - Writing a specific group and expecting the
*rules to still apply to that crawler.
Exercise
A file contains a group for * with Disallow: /tmp/, and a separate group for Googlebot with Disallow: /beta/. May Googlebot crawl /tmp/cache.html? Why?
Show answer
Yes. Googlebot uses only its own, more specific group, so the * rule Disallow: /tmp/ does not apply to it. Only /beta/ is blocked for Googlebot. To block /tmp/ for Googlebot too, add Disallow: /tmp/ to its group.
Exercise
Write a group for all crawlers that blocks /checkout/ but still allows /checkout/help/.
Show answer
User-agent: * Disallow: /checkout/ Allow: /checkout/help/