How to Write a robots.txt File: Rules, Wildcards, and 12 Common Formats

Views:
Create Time: 2026/11/30
Place the robots.txt file in the root directory to tell crawlers which paths to avoid. Basic syntax, wildcard rules, 12 common formats, and four common pitfalls are all covered here.

robots.txt is a plain-text file located in the root directory of a website that tells web crawlers which paths to crawl and which to avoid. It is not encrypted, nor does it provide access control—users can still access pages that are blocked; web crawlers simply do not crawl them.

I. What It Can Do

There are three common uses: to block access to areas that should not appear in search results, such as the backend, temporary files, and test directories; to direct crawlers to the sitemap; and to avoid dynamic URLs with complex parameters, thereby reducing invalid crawls.

There are also three things it cannot do: It cannot prevent others from accessing the page, so do not use it as a permission tool; it cannot guarantee that the page will not be indexed—if there are links elsewhere pointing to it, it may still appear in search results; and it cannot reduce server bandwidth consumption—visitors will continue to access the page as usual.

II. Basic Grammar

The file consists of several groups of entries, each beginning with "User-agent," followed by "Allow" and "Disallow." Entries beginning with "#" are comments.

  • User-agent: Specify which crawler this applies to; use * to select all. * can only appear once in a single file.
  • Disallow: Paths that are not allowed to be crawled, matched by prefix. /help will block /help.html, /helpabc.html, and /help/index.html.
  • Allow: Paths allowed for crawling. By default, all paths are allowed. This is typically used in conjunction with "Disallow" to specify exceptions.
  • * and $: Baidu's web crawler supports wildcards; * matches any character, and $ indicates the end of a string.

When there is a conflict between "Allow" and "Disallow," the "more specific rule takes precedence." For example, if "Disallow: /cgi-bin/" is combined with "Allow: /cgi-bin/see," only URLs beginning with "/cgi-bin/see" can be crawled.

III. 12 Common Writing Styles

  • Allow all: User-agent: * followed by Allow: /, or a blank Disallow
  • Block All: User-agent: * and Disallow: /
  • 只禁百度:User-agent: Baiduspider 加 Disallow: /
  • Allow only Baidu: User-agent: Baiduspider with an empty Disallow
  • Block certain directories: Disallow: /cgi-bin/, /tmp/, /admin/
  • Add an exception to the directory rules: Allow: /cgi-bin/see paired with Disallow: /cgi-bin/
  • Block all .htm files in a specific directory: Disallow: /cgi-bin/*.htm
  • Only allow files ending in .htm: Allow: .htm$ paired with Disallow: /
  • Block all dynamic pages containing parameters: Disallow: /*?*
  • Block Baidu from crawling images: Disallow: .jpg$, .jpeg$, .gif$, .png$, .bmp$
  • Only allow .gif files: Allow: .gif$, then block other formats one by one
  • 只禁百度抓 jpg:User-agent: Baiduspider 加 Disallow: .jpg$

IV. A Few Common Pitfalls

  • Paths are case-sensitive; if you misspell a single letter, the rule will not take effect.
  • Don't use "Disallow" on the robots.txt file itself, otherwise crawlers won't be able to read the rules.
  • The sitemap URL should be entered at this level, in the format: Sitemap: https://...
  • After making changes, run them through the Search Console's robots.txt validator first—don't publish them directly.

robots.txt is a communication file, not a control switch. For pages you truly don’t want to be indexed, in addition to using “Disallow,” you should also add “noindex”; doing both ensures reliability.

For web design in Wuxi, turn to Zongye Technology.


Helping You Build an Outstanding "Internet Plus" Enterprise
Zongye Official Account