Sub topic 1
How robots.txt works
Prerequisites
This chapter assumes you understand that a web crawler fetches pages over HTTP. You should also know that a crawler sends a User-Agent header, which identifies the software making the request. Basic familiarity with editing a website's files and using curl will help.
What robots.txt controls
robots.txt is a plain-text file that states which parts of a website a crawler may request. It implements the Robots Exclusion Protocol, a convention followed by well-behaved crawlers.
The file must be named exactly robots.txt and must be placed at the root of the host:
https://example.com/robots.txtA file at https://example.com/blog/robots.txt does not control the whole site. Rules apply only to the host and protocol where the file was found. For example, rules at https://example.com/robots.txt do not automatically control https://shop.example.com/robots.txt.
A crawler normally:
- Requests
/robots.txt. - Finds the rule group that matches its user-agent token.
- Tests the URL path against that group's rules.
- Fetches or skips the URL according to the result.
Groups and directives
A user-agent group starts with one or more User-agent lines. The group then contains Allow and Disallow directives. A blank line separates groups.
The value * is a wildcard group. It matches crawlers that do not have a more specific group.
User-agent: ExampleBot
Disallow: /private/
User-agent: *
Disallow: /drafts/
Allow: /This policy blocks ExampleBot from /private/. Other crawlers may access /private/, unless another rule applies. They may not access /drafts/, but they may access other paths.
An empty Disallow value means that nothing is blocked in that group:
User-agent: ExampleBot
Disallow:A / value blocks every path:
User-agent: ExampleBot
Disallow: /The path is matched from the beginning of the URL path. In the following example, /members/ and everything below it is disallowed, while /member-news/ is not necessarily matched because it does not begin with /members/.
User-agent: *
Disallow: /members/Matching and precedence
A crawler chooses the most specific user-agent group it supports. A named group such as GPTBot takes precedence over the wildcard group when that crawler recognizes the token.
Within a group, the most specific matching path rule wins. Specificity is based on the length of the matching path. If an Allow and Disallow rule match with equal specificity, the Allow rule is the less restrictive result and wins.
User-agent: *
Disallow: /articles/
Allow: /articles/public/This blocks /articles/private/ but allows /articles/public/guide. The longer Allow path is more specific.
robots.txt does not grant access or remove access controls. It only gives instructions before a crawler fetches a URL. It also does not hide a URL from people, search results, links, or other sources.
A basic policy file
The following file allows all crawlers except those explicitly assigned to /private/:
# Keep private paths out of compliant crawlers
User-agent: *
Disallow: /private/
# Explicitly allow a public area
Allow: /public/Comments begin with # and continue to the end of the line. Keep the file as plain text, serve it from the site root, and check that the server returns a successful response.
Exercise
A crawler matches both Disallow: /docs/ and Allow: /docs/public/. May it fetch /docs/public/start?
Show answer
Yes. The Allow path is longer and therefore more specific than /docs/, so it wins.