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 are grouped (2xx, 3xx, 4xx, 5xx), and how a site is organised into hosts and paths. It also assumes you can run a short Python script. The previous chapter separated crawling (requesting a URL) from indexing (storing a page in a search index). Here the question is narrower: where does a crawler look for robots.txt, and what does it do with what it finds?
One file per origin, at a fixed path
A crawler does not search your site for robots.txt. It requests one fixed location, the root of the host it is about to crawl:
https://www.example.com/robots.txtThe rules in that file apply only to the origin that served it. An origin is the combination of protocol, host name, and port. Each of these is a different origin with its own file:
| URL of the file | Rules apply to |
|---|---|
| https://www.example.com/robots.txt | URLs on https://www.example.com |
| https://example.com/robots.txt | URLs on https://example.com (no www) |
| https://blog.example.com/robots.txt | URLs on the blog subdomain only |
| http://www.example.com/robots.txt | URLs on plain HTTP only |
| https://www.example.com:8443/robots.txt | URLs on port 8443 only |
Three consequences follow:
- A file at
https://www.example.com/shop/robots.txtis never read. Only the root path counts. - Subdomains do not inherit rules from the parent domain. If
blog.example.comhas no file, the rules forwww.example.comdo not cover it. - If your site answers on both HTTP and HTTPS, or with and without
www, each variant is a separate origin. Most sites redirect all variants to one canonical origin, which makes the question mostly moot. Redirects are covered below.
The file name must be exactly robots.txt, in lowercase. Robots.TXT at the root is a different path and will not be found on a case-sensitive server.
The file format
robots.txt is a plain text file, served as UTF-8. Crawlers generally expect the text/plain content type, though they are lenient about it. The file is a list of lines. Most lines have the form field: value:
# Rules for all crawlers
User-agent: *
Disallow: /admin/
Sitemap: https://www.example.com/sitemap_index.xmlThe reading rules are short:
- Field names are case-insensitive.
User-agent,user-agent, andUSER-AGENTmean the same thing. - Values are case-sensitive. In
Disallow: /Admin/, the path/Admin/does not match/admin/. This is a common source of rules that silently do nothing. - Comments start with
#. Everything from the#to the end of the line is ignored. - Blank lines are allowed. They are conventionally used to separate groups of rules, which the later chapters on directives and group matching explain.
- Unknown lines are ignored. A crawler skips fields it does not support. This is why a typo such as
Dissallow:does not produce an error. The rule is just absent. - Invalid or unexpected content is skipped line by line. Parsers do not reject the whole file because of one bad line. A byte-order mark (BOM) at the start of the file is tolerated by major crawlers.
Crawlers also limit file size. Google, for example, processes only the first 500 KiB and ignores the rest. Rules past that point never take effect, so keep the file compact. Use pattern rules instead of listing thousands of individual URLs.
How a crawler fetches and uses the file
Before crawling a URL on an origin, a well-behaved crawler requests that origin's robots.txt, interprets the response, and only then decides whether the URL may be fetched. It does not fetch the file for every URL. It keeps a cached copy, and Google typically refreshes it at most about every 24 hours. A change you make is therefore not obeyed immediately, and a mistake can persist for hours after you fix it.
What the crawler does depends heavily on the HTTP status code of the response:
| Response | Typical crawler behavior |
|---|---|
| 200 with content | Parse the rules and obey them |
| 3xx redirect | Follow it, up to a limit (Google follows up to five hops). Too many redirects is treated like a 404 |
| 404 or other 4xx (except 429) | Treat as "no robots.txt": everything may be crawled |
| 429 or 5xx | Treat as a server problem: temporarily assume the whole site is disallowed, and retry later |
| Timeout or connection failure | Same as 5xx |
The two "error" rows behave in opposite directions, and that surprises many people. A missing file means open access. A broken file means closed access. The reasoning is that a missing file is a deliberate "no rules," while a server error means the crawler cannot tell what you want, so it plays safe. RFC 9309, the standard for the format, describes the same split.
Redirects and the canonical origin
Suppose http://example.com/robots.txt redirects to https://www.example.com/robots.txt. A crawler that follows the redirect will apply the rules it finds to the original host. This is the usual setup, and it is why you only need to maintain one real file. Cross-host redirects are handled in a crawler-specific way, so test the setup rather than assuming it. A long redirect chain risks hitting the hop limit and being treated as a missing file.
Computing the file's location in code
A crawler derives the robots.txt URL from any page URL by keeping the scheme and authority (host plus port) and replacing everything else. The following script does this with the standard library:
from urllib.parse import urlsplit, urlunsplit
def robots_url(page_url):
parts = urlsplit(page_url)
return urlunsplit((parts.scheme, parts.netloc, "/robots.txt", "", ""))
pages = [
"https://www.example.com/blog/first-post?utm=1",
"http://www.example.com/",
"https://blog.example.com/2026/entry",
"https://www.example.com:8443/app",
]
for page in pages:
print(page, "->", robots_url(page))Running it prints:
https://www.example.com/blog/first-post?utm=1 -> https://www.example.com/robots.txt
http://www.example.com/ -> http://www.example.com/robots.txt
https://blog.example.com/2026/entry -> https://blog.example.com/robots.txt
https://www.example.com:8443/app -> https://www.example.com:8443/robots.txtThe path, query string, and fragment are discarded. The scheme and port are preserved, which matches the origin rules in the table above.
Reading the rules with a parser
Python ships a robots.txt parser in urllib.robotparser. Feeding it lines directly lets you test rules without any network access:
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. To fetch a live file instead, call rp.set_url("https://www.example.com/robots.txt") followed by rp.read(). Be aware that this parser makes its own choices about status codes. It treats 401 and 403 as "disallow everything" and other 4xx codes as "allow everything." That differs from Google, which treats 4xx (except 429) as allow-all. The parser also does not implement every wildcard feature that major search engines support. Use it to learn and to sanity-check simple rules, but verify important rules with the crawler vendor's own tester. Testing is covered in the final chapter.
Common mistakes with location and reading
- Uploading the file to the wrong place. A file in a subfolder, or a CMS that serves it at
/robots.txt/or/robots, is not found. Request the exact URL in a browser and check the status code. - Forgetting subdomains. Staging, blog, and shop subdomains each need their own file if you want rules there.
- Serving an HTML page. Some frameworks return a styled "not found" page with status 200 for unknown paths. The crawler then tries to parse HTML as rules. Make sure a missing file really returns 404.
- Wrong case in paths. Remember that field names are not case-sensitive but path values are.
- Expecting instant effect. Cached copies mean changes take hours to be picked up.
- Treating it as binding. Compliance is voluntary. Reputable crawlers obey the file. Others ignore it, which is why robots.txt is not a security measure, a point a later chapter develops.
Exercise
Your site serves pages on https://www.example.com and https://shop.example.com. You publish a robots.txt at https://www.example.com/robots.txt that contains "Disallow: /cart/". A week later you find crawlers fetching https://shop.example.com/cart/ freely. Why, and what is the fix? Also, what would a crawler do if https://shop.example.com/robots.txt began returning a 503 status for several hours?
Show answer
robots.txt applies only to the origin that serves it, and shop.example.com is a different origin from www.example.com, so the rule never reached it. The fix is to publish a robots.txt at https://shop.example.com/robots.txt containing the rule. If that file returned 503, a crawler would treat it as a server problem, temporarily assume the whole shop host is disallowed, and retry later. Crawling of the shop would pause until the file responds normally.
Exercise
Using the robots_url function from this chapter, what does it return for "https://Example.com:443/a/b.html#top"? And does a missing robots.txt (404) mean crawlers will avoid your site or crawl all of it?
Show answer
It returns "https://Example.com:443/robots.txt". The function keeps the scheme and the network location exactly as given and drops the path, query, and fragment. A 404 on robots.txt means there are no rules, so crawlers treat the whole origin as allowed to crawl.