How do you write a robots.txt file?
What a robots.txt file does
A robots.txt file tells crawlers which URLs on a site they are allowed to request. It follows the Robots Exclusion Protocol, which became an internet standard as RFC 9309 in September 2022. Well behaved crawlers, including Googlebot, check the file before crawling and follow its rules.
Two limits are worth knowing from the start. First, robots.txt controls crawling, not indexing: Google explains that a disallowed URL can still be indexed if it is linked from elsewhere. Second, it is not access control. RFC 9309 states that the rules are not a form of access authorisation, and the file is public, so never use it to hide private content.
Where the file goes
The file must be called `robots.txt` and sit in the top level directory of the host, for example `https://www.example.com/robots.txt`. Rules only apply to the exact protocol, host and port where the file is served, so `https://shop.example.com` needs its own file. Crawlers do not look for robots.txt files in subdirectories.
Save it as plain UTF-8 text. Google processes up to 500 KiB of the file and ignores anything after that.
The rules
Google supports four fields:
- `user-agent` names the crawler a group of rules applies to. Use `*` for all crawlers.
- `disallow` gives a path the crawler should not request.
- `allow` gives a path the crawler may request, used to make exceptions inside a disallowed section.
- `sitemap` gives the full URL of an XML sitemap. It is not tied to any user agent group.
Field names are not case sensitive, but paths are. Each path should start with `/`. Two wildcards are supported: `*` matches any sequence of characters and `$` marks the end of a URL.
How Google decides which rule applies
A crawler follows only one group, the one whose user agent most specifically matches its name. Within that group, Google applies the most specific matching rule, measured by the length of the path. If an allow and a disallow rule are equally specific, Google uses the less restrictive one, which is the allow rule.
Examples
A typical small site that keeps crawlers out of an admin area and a search results path:
``` User-agent: * Disallow: /admin/ Disallow: /search Allow: /admin/help/
Sitemap: https://www.example.com/sitemap.xml ```
Blocking one file type for Googlebot only:
``` User-agent: Googlebot Disallow: /*.pdf$ ```
Blocking the whole site for every crawler, which is useful on a staging copy but disastrous on a live site:
``` User-agent: * Disallow: / ```
How the server response matters
Google treats the status code of robots.txt as part of the instruction:
- A 2xx response means the file is read as written.
- A 3xx redirect is followed up to five times.
- A 4xx response, other than 429, is treated as if there were no restrictions.
- A 5xx response makes Google pause crawling for a time and reuse the last good copy, for up to 30 days.
Google normally caches the file for up to 24 hours, so changes are not picked up instantly.
Common mistakes
- Leaving `Disallow: /` in place after launching a site that was built on a staging server.
- Blocking CSS or JavaScript files that pages need to render.
- Using robots.txt to remove pages from search instead of noindex.
- Forgetting that each subdomain needs its own file.
- Relying on unsupported fields such as crawl-delay for Googlebot.
Check your robots.txt
Our LLMs.txt Auditor is built for llms.txt files, but it checks your robots.txt alongside llms.txt and llms-full.txt, and flags a noindex header that conflicts with the llms.txt file. Use it as a check that the two files work together, not as a full robots.txt validator.
Sources
- How Google interprets the robots.txt specification
- Useful robots.txt rules
- RFC 9309: Robots Exclusion Protocol
- Does Anthropic crawl data from the web, and how can site owners block the crawler?
Frequently asked questions
Does a robots.txt file stop a page appearing in Google?
Not reliably. robots.txt controls crawling, not indexing. Google explains that a URL blocked in robots.txt can still be indexed, without a description, if other pages link to it. To keep a page out of search results, allow it to be crawled and use a noindex robots meta tag or header, or protect it with a password.
Does Google support Crawl-delay in robots.txt?
No. Google supports only the user-agent, allow, disallow and sitemap fields, and ignores others such as crawl-delay. Some other crawlers, including Anthropic's, do honour crawl-delay. Adding crawl-delay will not slow Googlebot, so if you need it for other crawlers, place it only in their user agent groups and do not expect any effect on Google.