Sub topic 4
Matching URLs with * and $
This chapter assumes you can read a basic robots.txt file with User-agent, Disallow, and Allow lines, and that you know a URL has a path and a query string. Here you will learn the two pattern characters that robots.txt supports and how to predict what they match.
How matching works by default
A rule's value is a path pattern. A crawler compares it to the start of the URL's path plus its query string. This is a prefix match. Disallow: /search blocks /search, /search/, /searchresults, and /search?q=shoes.
Two patterns extend this. Google, Bing, and most major crawlers support both. They are part of the Robots Exclusion Protocol standard (RFC 9309).
| Character | Meaning |
|---|---|
| `*` | Matches any sequence of characters, including none |
| `$` | Matches the end of the URL. It only has this meaning at the end of a pattern |
Patterns are case-sensitive. Disallow: /Admin/ does not block /admin/.
The * wildcard
Use * when the part you want to match is in the middle of a URL. This rule blocks any URL that contains a session ID parameter:
User-agent: *
Disallow: /*?sessionid=The pattern starts at /, allows anything (*), and then requires ?sessionid=. It matches /products?sessionid=abc and /blog/post?sessionid=9.
A trailing * adds nothing, because matching is already by prefix. Disallow: /private* and Disallow: /private behave the same.
The $ end anchor
Without $, a pattern matches every URL that begins with it. With $, the URL must end exactly there. This rule blocks PDF files:
User-agent: *
Disallow: /*.pdf$It matches /files/report.pdf. It does not match /files/report.pdf?download=1, because that URL does not end in .pdf. It also does not match /files/report.pdfs. If you want to catch the query-string variant, add a second rule: Disallow: /*.pdf?.
Checking your own patterns
The Python standard library parser follows the older, simpler rules. It does not understand * or $ inside paths, so do not use it to test wildcard rules. It is fine for plain prefix rules:
from urllib.robotparser import RobotFileParser
rules = [
"User-agent: *",
"Disallow: /private/",
]
rp = RobotFileParser()
rp.parse(rules)
print(rp.can_fetch("*", "https://www.example.com/private/report.html"))
print(rp.can_fetch("*", "https://www.example.com/blog/"))The first call prints False and the second prints True. For wildcard rules, test with a tool that implements the current standard, such as the robots.txt report in Google Search Console.
Common mistakes
- Putting
$in the middle of a pattern. It is then read as a literal character in some parsers, so the rule does not do what you intended. - Forgetting the leading
/. Patterns should begin with/or*. - Assuming
*is a regular expression. Only*and$are special. A pattern like/page[0-9]matches those characters literally. - Writing a broad
*rule and blocking more than you meant.Disallow: /*?blocks every URL with a query string, including ones you may want crawled.
Exercise
A site has the rule Disallow: /*.pdf$. Does it block https://www.example.com/docs/guide.pdf?version=2? Explain why, and write one extra rule that would block it.
Show answer
No. The $ requires the URL to end with .pdf, and this URL ends with ?version=2. Adding Disallow: /*.pdf? blocks PDF URLs that carry a query string.