How should robots.txt be written?
robots.txt is a plain text file in a website’s root directory that controls search engine crawler access. Below are its core syntax, common cases and cautions in detail:
1. Basic syntax and structure
- File location and naming
- It must be in the site’s root directory (e.g.
https://www.example.com/robots.txt), and the filename must be strictly lowercase. - If the file does not exist, crawlers may access the whole site by default.
- It must be in the site’s root directory (e.g.
- Core directives
User-agent: specifies the target crawler (e.g.Googlebot,Baiduspideror*for all crawlers).Disallow: paths that must not be accessed (e.g./admin/blocks crawling theadmindirectory).Allow: paths that may be accessed (used to overrideDisallowrules).Sitemap: optional; declares the sitemap path (must be a full URL).
- Wildcards and matching rules
*: matches any characters (e.g./private*/blocks all directories starting withprivate).$: matches the end of a URL (e.g.Disallow: /*.jpg$blocks all.jpgimages).- Paths are case-sensitive, and a trailing slash changes the scope (e.g.
/images/blocks the whole directory, while/imagesblocks only the file of that name).
2. Common case examples
- Block all crawlers
User-agent: * Disallow: /Effect: no content on the site is indexed. - Allow a specific crawler
User-agent: BadBot Disallow: / User-agent: * Allow: /Effect: onlyBadBotmay access the whole site. - Restrict directories and file types
User-agent: * Disallow: /cgi-bin/ Disallow: /images/*.gif Allow: /public/Effect: thecgi-bindirectory and all GIF images are blocked, thepublicdirectory is allowed. - Dynamic pages and parameter filtering
User-agent: * Disallow: /*?*Effect: URLs with query parameters (e.g.index.php?id=1) are not crawled.
3. Advanced usage and cautions
- Per-crawler rules
User-agent: Googlebot Disallow: /temp/ Allow: /public/ User-agent: Baiduspider Disallow: /admin/Each crawler can have its own rules. - Supplementary Meta tags Control the crawling behaviour of a single page further in HTML with
<meta name="robots" content="noindex, nofollow">. - Common mistakes and tuning
- File encoding: ANSI or UTF-8 is recommended (to avoid garbled characters on the first line).
- Path conventions: avoid stray spaces at the end of a path or confusion over slashes.
- Testing tools: check the syntax with the Robots.txt tester in Google Search Console.
4. Complete example template
User-agent: *
Disallow: /admin/
Disallow: /private/
Disallow: /*.php$
Allow: /public/
Sitemap: https://www.example.com/sitemap.xml
5. Cautions
- Not mandatory: robots.txt is an advisory protocol, and some crawlers may ignore its rules.
- Privacy: sensitive information still needs server-side access control rather than reliance on robots.txt.
- Updates take time: after a change, wait for search engines to refresh their cache (usually hours to days).
With a sensible robots.txt you can manage how far search engines crawl, and improve site performance and privacy. Combining it with a sitemap (Sitemap) and Meta tags gives finer control.