Sub topic 2
Where robots.txt lives and how it is read
This chapter assumes you know what a URL is, how HTTP status codes work, and how a site is organized into hosts, paths, and directories. It covers where a crawler looks for robots.txt, how it fetches the file, and how the response changes crawler behavior. Rule syntax comes in the next chapter.
Location: one file per origin
A crawler requests robots.txt from the root of each origin. An origin is the combination of scheme, host, and port. The file applies only to that origin.
| Site you crawl | File the crawler requests |
|---|---|
| `https://www.example.com/blog/post` | `https://www.example.com/robots.txt` |
| `http://www.example.com/` | `http://www.example.com/robots.txt` |
| `https://shop.example.com/` | `https://shop.example.com/robots.txt` |
| `https://www.example.com:8443/` | `https://www.example.com:8443/robots.txt` |
Each row is a separate origin with its own file. A file at https://www.example.com/robots.txt does not cover shop.example.com, the http:// version of the site, or another port. A file placed at /blog/robots.txt is never requested.
Format requirements
The file is plain text, UTF-8 encoded, and served with a text/plain content type. Each line is a field: value pair. A # starts a comment that runs to the end of the line. Field names are not case-sensitive, but the paths in values are case-sensitive. Google reads only the first 500 KiB of the file and ignores anything after that, so keep it small.
How a crawler fetches and reads it
Crawlers fetch the file before crawling other URLs on the origin. They cache the result, often for up to 24 hours, so a change you publish may take a while to take effect.
The status code decides how the response is treated:
- 2xx: The crawler parses the body and applies the rules.
- 3xx: The crawler follows a limited number of redirects, about five for Google. If it never reaches a final response, it treats the file as missing.
- 4xx (such as 404): The crawler treats the file as absent and assumes no restrictions. A site with no
robots.txtis fully crawlable. - 5xx or a timeout: The crawler assumes the server has a problem. Google stops crawling the site for a while and retries. If errors persist for a long time, it may fall back to the last cached copy or, eventually, treat the file as missing.
The 5xx behavior is the common trap. A broken server or a misconfigured firewall that returns errors for /robots.txt can halt crawling of the whole site, even though you never wrote a rule.
Reading the file yourself
Python's standard library can fetch and parse a live file, which helps you see what a crawler sees.
from urllib.robotparser import RobotFileParser
rp = RobotFileParser("https://www.example.com/robots.txt")
rp.read() # fetches the file; a 4xx marks all URLs as allowed
print(rp.can_fetch("*", "https://www.example.com/blog/"))read() applies the same status logic described above in simplified form. Note that can_fetch answers a narrow question about crawling. It says nothing about whether a page will appear in search results. That distinction is covered later in this tutorial.
Common mistakes
- Putting the file in a subdirectory.
- Forgetting that
wwwand non-wwwhosts, orhttpandhttps, each need their own file or a redirect to a single canonical one. - Serving the file as HTML, for example a custom 404 page returned with status 200.
- Blocking the file itself with a firewall or login wall.
Exercise
A site returns HTTP 503 for https://www.example.com/robots.txt during a server outage. The file has no rules that block anything. What is the likely effect on crawling, and why?
Show answer
Crawlers will probably pause crawling the whole site and retry later. A 5xx response signals a server problem, not a missing file, so the crawler cannot assume that everything is allowed. The absence of blocking rules does not matter because the rules were never read.
Exercise
You publish a rule file at https://www.example.com/blog/robots.txt to restrict only the blog. Will it work?
Show answer
No. Crawlers request robots.txt only from the root of the origin, https://www.example.com/robots.txt. A file in a subdirectory is never requested. Put the rules in the root file, using paths such as /blog/.