How to Write a robots.txt File (With Examples)
Web & SEO · Published October 11, 2026 · Updated October 24, 2026 · 3 min read
Written by Akinrinmade Michael
A robots.txt file is a plain text file at the root of your site that tells crawlers which parts they may visit. Well-behaved crawlers, such as search engines, read it first. It's a request, not a lock, so it doesn't stop anyone determined.
This guide is about deciding what belongs in the file and how it fits with the other ways of steering search engines. If you want to build a file right away, the Robots.txt Generator has three ready-made setups (a blog, a shop and a staging site) you can copy under the tool.
The basic format
User-agent: * Disallow: /admin/ Disallow: /private/ Sitemap: https://example.com/sitemap.xml
User-agentnames the crawler.*means all of them.Disallowlists a path crawlers shouldn't visit.Allowmakes an exception inside a blocked folder.Sitemappoints to your sitemap with a full address. You can list more than one.- Lines starting with
#are comments.
What belongs in the file
The goal is to keep crawlers from wasting time on pages with no value in search, so they spend it on the pages that matter. That usually means:
- Login, account, cart and checkout pages
- Internal search results, which can generate endless URLs
- Filter and sort variations that show the same products in a different order
- Admin areas and scripts you don't want crawled
- Duplicate versions of a page, such as print-friendly copies
A small site with a dozen pages often needs no blocks at all. A file with just a sitemap line is perfectly fine.
Three tools, three jobs
People mix up robots.txt, the noindex tag and the canonical tag. They solve different problems:
| Tool | What it controls | Use it when |
|---|---|---|
| robots.txt | Whether a crawler may fetch a URL | You want to save crawl time on pages that don't matter |
| noindex meta tag | Whether a page may appear in results | A page should be reachable but not listed, like a thank-you page |
| Canonical tag | Which version of near-identical pages counts | The same content is reachable from several addresses |
| Password or login | Who can see the page at all | The content is private |
The two that clash most often are robots.txt and noindex. If you block a page in robots.txt, the crawler never fetches it, so it never sees a noindex tag on it. To keep a page out of results, allow crawling and add noindex. The Meta Tag Generator covers the tags, and the SEO meta tags guide explains how titles and descriptions fit in.
Common examples
- Allow everything:
User-agent: *followed by an emptyDisallow:line. - Block everything:
Disallow: /. Use this only on a staging site. - Block one crawler: add a separate group with its name, such as
User-agent: GPTBot, thenDisallow: /. - Block one file type:
Disallow: /*.pdf$for every URL that ends in .pdf.
What robots.txt doesn't do
Blocking a page doesn't remove it from search results. If other sites link to it, the address can still appear without a description. Also, don't list secret folders in robots.txt, because the file is public and anyone can read it. Listing /secret-admin/ in it tells every visitor exactly where to look.
Mistakes to avoid
- Leaving
Disallow: /in place after launching a site. This is the most common reason a new site never appears in search. - Blocking CSS and JavaScript folders, which stops search engines from rendering your pages properly.
- Putting the file in a subfolder. It must load at
yourdomain.com/robots.txt. - Expecting every crawler to obey
Crawl-delay. Google ignores it, though some other crawlers honor it. - Forgetting that each subdomain needs its own file.
A launch checklist
- Build the file with the Robots.txt Generator and upload it to the root of the live site.
- Open
yourdomain.com/robots.txtin a browser and confirm it loads as plain text. - Check that none of your important pages are blocked by a rule that's broader than you meant.
- Add your sitemap line, and submit the sitemap in the search engine's webmaster tools.
- Look at the file again after every site rebuild or move. On Apache, the .htaccess Generator can set up redirects when addresses change.