Sub topic 9
Testing your rules
This chapter assumes you can read short Python scripts, run curl, and edit the robots.txt file at the root of your site. It also assumes you have a draft policy from the previous chapters, such as the OpenAI, Google-Extended, ClaudeBot, CCBot and Applebot-Extended groups. Testing should answer three questions in order:
- Does the draft file say what you meant?
- Is the live file the one crawlers actually receive?
- Does the server behave as expected when a request claims to be a given crawler?
A rule that looks right can still fail on any of these.
A testing workflow
Treat robots.txt like code. Write down the outcomes you expect, check the draft against them, deploy, then check the deployed file and the server.
Step 1: write expectations first
Before you run anything, list the (user agent, URL, allowed?) combinations your policy implies. For the policy in this tutorial, which opts out of training but stays open to AI search and user-initiated fetches, the expectations look like this:
| User agent | Path | Expected |
|---|---|---|
| GPTBot | /articles/guide | blocked |
| Google-Extended | /articles/guide | blocked |
| ClaudeBot | /articles/guide | blocked |
| OAI-SearchBot | /articles/guide | allowed |
| ChatGPT-User | /articles/guide | allowed |
| Googlebot | /articles/guide | allowed |
| SomeOtherBot | /articles/guide | allowed |
Include a control row such as SomeOtherBot. It confirms that the User-agent: * group, or the absence of any matching group, behaves as you think. Include a search crawler you want to keep, too. The most expensive mistake is a typo that blocks the wrong thing.
Step 2: check the draft with a parser
Python's standard library includes urllib.robotparser. The script below loads either a local draft or the live file and compares every result with the expectations. It exits with a non-zero status on any mismatch, so you can run it in CI before deploying.
import sys
import urllib.request
from urllib.robotparser import RobotFileParser
SITE = "https://example.com"
# (user agent, path, should the agent be allowed to fetch it?)
EXPECTED = [
("GPTBot", "/articles/guide", False),
("Google-Extended", "/articles/guide", False),
("ClaudeBot", "/articles/guide", False),
("CCBot", "/articles/guide", False),
("Applebot-Extended", "/articles/guide", False),
("OAI-SearchBot", "/articles/guide", True),
("ChatGPT-User", "/articles/guide", True),
("Googlebot", "/articles/guide", True),
("SomeOtherBot", "/articles/guide", True),
]
def load_rules(source=None):
"""Read a local draft if a path is given, otherwise fetch the live file."""
if source:
with open(source, encoding="utf-8") as handle:
return handle.read()
request = urllib.request.Request(
SITE + "/robots.txt", headers={"User-Agent": "robots-check/1.0"}
)
with urllib.request.urlopen(request, timeout=10) as response:
return response.read().decode("utf-8")
def main():
source = sys.argv[1] if len(sys.argv) > 1 else None
parser = RobotFileParser()
parser.parse(load_rules(source).splitlines())
failures = 0
for agent, path, expected in EXPECTED:
actual = parser.can_fetch(agent, SITE + path)
status = "ok " if actual == expected else "FAIL"
if actual != expected:
failures += 1
print(f"{status} {agent:<18} {path:<18} allowed={actual}")
sys.exit(1 if failures else 0)
if __name__ == "__main__":
main()Save your draft as robots.txt.draft and run python check_robots.py robots.txt.draft. Run it with no argument after deployment to test the live file.
The script fetches the file itself with urllib.request and passes the text to parse(). It does not call parser.read(). The read() method sends Python's default user agent, and some CDNs and firewalls answer that with 403. The parser then treats a 403 as "disallow everything" and your test fails for a reason unrelated to your rules. Fetching explicitly lets you set the user agent and see the real HTTP error.
Where the stdlib parser differs from real crawlers
The standard-library parser is a good check for typos and group structure, but it does not match the Robots Exclusion Protocol (RFC 9309) in every detail.
- Rule precedence. RFC 9309 says the most specific (longest) matching rule wins. The stdlib parser applies the first matching rule in file order.
- Wildcards. Major crawlers support
*and$in paths. The stdlib parser may treat them as literal characters, depending on the Python version. - Agent matching. A crawler compares its own product token to the group name. The stdlib parser checks whether the group name appears anywhere inside the agent string, so a group named
GPTwould matchGPTBotin Python but not for the real crawler.
The next script shows the precedence difference.
from urllib.robotparser import RobotFileParser
def check(rules, url):
parser = RobotFileParser()
parser.parse(rules.splitlines())
return parser.can_fetch("GPTBot", url)
url = "https://example.com/members/public/faq"
disallow_first = """\
User-agent: GPTBot
Disallow: /members/
Allow: /members/public/
"""
allow_first = """\
User-agent: GPTBot
Allow: /members/public/
Disallow: /members/
"""
print(check(disallow_first, url)) # False in Python; True under RFC 9309
print(check(allow_first, url)) # True in bothFor a second opinion on complex rules, use the robots.txt report in Google Search Console or another parser that implements RFC 9309. The tool matters less than checking against more than one implementation when rules overlap.
Step 3: verify the live file with curl
A correct draft proves nothing if the file is not served correctly. Check the status code, content type, redirects and body of the deployed file:
# Status line and headers: expect 200 and a text/plain content type
curl -sI https://example.com/robots.txt | head -n 5
# Follow redirects and report the final URL and status
curl -sL -o /dev/null -w "%{http_code} %{url_effective}\n" https://example.com/robots.txt
# Read the body exactly as a crawler will receive it
curl -s https://example.com/robots.txtLook for these problems:
- Wrong location. The file must be at the root of each host.
https://example.com/robots.txtdoes not coverhttps://blog.example.com/, which needs its own file. Protocol and port are also part of the origin. - Not 200. Under RFC 9309, a 4xx response means the file is unavailable and crawlers may assume no restrictions. A 5xx or unreachable server means crawlers should assume everything is disallowed. A deployment that breaks the file can silently open or close your whole site.
- HTML instead of text. A single-page app or a custom 404 page that returns 200 with HTML gives crawlers a file full of markup and no rules.
- Stale cache. Crawlers cache
robots.txt, often for up to a day. A CDN can also serve an old copy. Compare the body with your draft, and expect a delay before crawlers notice a change. - Byte problems. Use UTF-8. Crawlers apply size limits, and Google, for example, reads only the first 500 KiB. Keep the file small.
Step 4: test the server's behavior
If you added server-level blocks, such as the nginx map from earlier, test them separately. robots.txt rules never produce an error response. The server only enforces what you configured. Send requests that claim to be a crawler:
# Should return 403 if the server blocks GPTBot
curl -s -o /dev/null -w "%{http_code}\n" -A "GPTBot/1.1" https://example.com/articles/guide
# Should return 200 for a crawler you allow
curl -s -o /dev/null -w "%{http_code}\n" -A "OAI-SearchBot/1.0" https://example.com/articles/guide
# Should return 200 for the robots.txt file itself, even for a blocked agent
curl -s -o /dev/null -w "%{http_code}\n" -A "GPTBot/1.1" https://example.com/robots.txtThe third command checks that a server-level block does not also cover /robots.txt. If a blocked crawler receives 403 for the rules file, it cannot read your instructions. Under RFC 9309 it may then treat the whole site as unrestricted.
A user-agent test only shows what the server does with an honest claim. Anyone can send -A "GPTBot", so a pass here does not prove the real crawler is who it says it is. Verification by IP range is covered in the enforcement chapter.
Common failures
| Symptom | Likely cause | Fix |
|---|---|---|
| A blocked crawler still passes the parser test | Group name misspelled, for example `GPT-Bot` | Copy the token exactly from the vendor documentation |
| Every agent is blocked in a test | `Disallow: /` under `User-agent: *`, or the parser got a 403 on fetch | Check the `*` group; fetch with a custom user agent |
| A rule seems ignored | Rules placed before any `User-agent` line, or a blank line splitting a group | Keep each group's lines together under its user-agent lines |
| A specific agent ignores the `*` group | A crawler uses the most specific matching group only, not both | Repeat shared rules inside the specific group |
| The live file differs from the draft | CDN cache or an old deploy | Purge the cache and fetch again with `curl` |
| Parser says allowed, but the server returns 403 | A server rule or firewall blocks that user agent | Compare the nginx or WAF rules with the intended policy |
The fourth row catches many people. If GPTBot has its own group, it ignores User-agent: * entirely. A Disallow: /private/ that exists only under * does not apply to GPTBot, and your parser test will show it as allowed.
Re-test after every change
Run the same expectation list whenever you add a crawler token, edit paths, or change CDN or server configuration. Keep the script and the draft file in version control next to the site. A small failing test is much cheaper than discovering weeks later in the logs that a crawler you meant to block kept fetching pages.
Exercise
Your draft contains "User-agent: *" with "Disallow: /drafts/", plus a separate "User-agent: GPTBot" group with "Disallow: /members/". A parser test shows GPTBot is allowed to fetch /drafts/page. Why, and how do you fix it?
Show answer
A crawler uses only the single most specific group that matches its name. GPTBot has its own group, so it ignores the "*" group and the /drafts/ rule never applies to it. Add "Disallow: /drafts/" to the GPTBot group, then rerun the test.
Exercise
You deploy a new nginx rule that returns 403 for any request whose User-Agent contains "ClaudeBot". Which curl command checks that you did not accidentally block the rules file, and what result do you want?
Show answer
Run curl -s -o /dev/null -w "%{http_code}\n" -A "ClaudeBot/1.0" https://example.com/robots.txt. You want 200. A 403 would stop the crawler from reading your rules, and it may then treat the site as having no restrictions.