A robots.txt file tells crawlers which parts of your site they may request. It is short and easy to get wrong in ways that quietly hurt your search traffic, so it pays to know exactly what each line does.
Where it goes and how it is read
The file must be served as plain text at the root of the host, such as https://example.com/robots.txt. Each subdomain and each protocol needs its own file. Crawlers fetch it before they crawl and apply the group that best matches their name.
Groups and rules
A group starts with one or more User-agent lines, followed by Allow and Disallow rules for that crawler. User-agent: * applies to every crawler without a more specific group. Disallow: with an empty value allows everything, while Disallow: / blocks the whole site.
Paths are prefixes, so Disallow: /private blocks /private, /private/ and /private-notes. Major crawlers also support * as a wildcard and $ to anchor the end of a URL, and when rules conflict the most specific one wins.
Sitemaps and crawl delay
Add one Sitemap: line with an absolute URL for each sitemap. It is independent of any group. Crawl-delay is honoured by some crawlers but ignored by Google, so do not rely on it to control load.
What robots.txt cannot do
It is a request, not access control: compliant crawlers follow it, and anyone can still open a blocked URL. It also does not remove pages from search results. A blocked URL can still be indexed from external links, and because the crawler cannot fetch the page it cannot see a noindex tag on it. To keep a page out of results, allow crawling and use a noindex meta tag or header instead.