Sub topic 4
Matching URLs with * and $
Crawlers match every rule in a robots.txt file against a URL path. By default the match is a plain prefix comparison. Two special characters, * and $, extend it. This chapter covers what each character does, how they interact with query strings, and how to predict which rule wins when several match.
It assumes you can write basic User-agent, Disallow, and Allow lines, as covered in the previous chapter. It also assumes you know that a URL has a path and an optional query string.
The default: prefix matching
Without special characters, a rule matches any URL whose path starts with the rule's value.
User-agent: *
Disallow: /tmpThis blocks /tmp, /tmp/, /tmp/file.txt, and also /tmpfiles/report.html. The rule is a prefix, not a directory name. Adding a trailing slash (/tmp/) limits the rule to that directory.
Three details apply to all matching:
- The path is compared case-sensitively.
Disallow: /Admin/does not block/admin/. - The query string is part of what gets compared. For
/search?q=shoes, the crawler matches against/search?q=shoes, not just/search. - Only the path is matched, not the scheme or host. Rules never contain
https://www.example.com.
The two special characters
Only two characters have special meaning in a rule path.
| Character | Meaning |
|---|---|
| `*` | Matches any sequence of characters, including none and including `/` |
| `$` | At the very end of a rule, means the URL must end exactly there |
Everything else is literal. In particular, ? is an ordinary character, not a regex "optional" marker. Disallow: /*?sessionid= matches a literal question mark. Character classes like [a-z] and alternation like a|b are not supported.
The wildcard `*`
* can appear anywhere in the path, any number of times.
User-agent: *
Disallow: /*?sessionid=
Disallow: /*/drafts/
Disallow: /images/*.tmp/*?sessionid=blocks any URL with?sessionid=after any path, such as/products?sessionid=abc123./*/drafts/blocks/blog/drafts/post-1and/news/2026/drafts/x. It does not block/drafts/x, because the rule needs a/beforedraftsthat comes after at least the leading slash./images/*.tmpblocks/images/a.tmpand/images/2026/b.tmp.bak, since without$the rule is still a prefix match.
A trailing * adds nothing. Disallow: /private* and Disallow: /private behave identically, because prefix matching already covers whatever follows. Crawlers ignore the redundant star, so there is no harm in it, but it is clutter.
The end anchor `$`
$ turns a prefix match into an exact-ending match. It only has this meaning at the end of a rule.
User-agent: *
Disallow: /*.pdf$This blocks /files/report.pdf. It does not block /files/report.pdf?download=1, because that URL does not end with .pdf. This is the most common surprise with $: since the query string counts as part of the URL, a trailing parameter defeats the anchor.
If you want to block PDFs whether or not they have a query string, drop the $:
Disallow: /*.pdfThe cost is that this also blocks /guides/intro.pdf-viewer and similar URLs that merely contain .pdf. Choose based on your URL structure.
Common patterns
| Rule | Blocks | Does not block |
|---|---|---|
| `Disallow: /*?` | Every URL with a query string | `/page` |
| `Disallow: /*?*sort=` | `/shop?sort=price`, `/shop?page=2&sort=price` | `/shop?page=2` |
| `Disallow: /*.json$` | `/api/data.json` | `/api/data.json?v=2` |
| `Disallow: /$` | Only the home page `/` | `/about` |
| `Disallow: /*/print/` | `/blog/post/print/` | `/print/` |
The Disallow: /$ row is a useful trick. Disallow: / blocks the entire site, but /$ blocks only the exact root URL. It appears most often paired with Allow rules.
Checking your understanding with code
Python's urllib.robotparser is convenient for plain rules, but it compares prefixes only. It does not implement * or $ inside paths, so it gives wrong answers for the rules in this chapter. To experiment, you can translate a rule into a regular expression yourself.
The script below does that. It splits the rule on *, escapes each literal piece, joins the pieces with .*, and adds an end anchor if the rule ended with $. It then applies the precedence rules described in the next section.
import re
def to_regex(pattern):
anchored = pattern.endswith("$")
if anchored:
pattern = pattern[:-1]
body = ".*".join(re.escape(part) for part in pattern.split("*"))
return re.compile("^" + body + ("$" if anchored else ""))
def is_allowed(rules, path):
best = None # (rule length, is_allow)
for directive, pattern in rules:
if pattern == "":
continue # an empty rule matches nothing
if to_regex(pattern).match(path):
key = (len(pattern), directive == "allow")
if best is None or key > best:
best = key
return True if best is None else best[1]
rules = [
("disallow", "/admin/"),
("allow", "/admin/help/"),
("disallow", "/*?sessionid="),
("disallow", "/*.pdf$"),
]
for path in [
"/blog/",
"/admin/users",
"/admin/help/faq",
"/products?sessionid=abc",
"/files/report.pdf",
"/files/report.pdf?download=1",
]:
print(path, "->", "allowed" if is_allowed(rules, path) else "blocked")Expected output:
/blog/ -> allowed
/admin/users -> blocked
/admin/help/faq -> allowed
/products?sessionid=abc -> blocked
/files/report.pdf -> blocked
/files/report.pdf?download=1 -> allowedThis is a teaching tool, not a full implementation. It ignores percent-encoding normalization and user-agent groups. For production checks, use the testing tools covered in a later chapter.
Which rule wins
A single URL can match several rules, including both Allow and Disallow. Under the Robots Exclusion Protocol (RFC 9309), the crawler applies this order:
- Find all rules in the selected group that match the URL.
- Pick the most specific one, meaning the rule with the longest path, counted in characters including any
*and$. - If an
Allowand aDisallowtie on length,Allowwins. - If no rule matches, the URL is allowed.
Rule order in the file does not matter. Putting Allow before Disallow changes nothing.
User-agent: *
Disallow: /admin/
Allow: /admin/help/For /admin/help/faq, both rules match. /admin/help/ is 12 characters and /admin/ is 7, so the Allow wins. For /admin/users, only the Disallow matches.
A tie that surprises people
Length counts the rule text, not how many URLs it matches. A wildcard rule can beat a short, specific-looking one:
User-agent: *
Disallow: /downloads/
Allow: /*.pdf$For /downloads/guide.pdf, /downloads/ is 11 characters and /*.pdf$ is 7. The Disallow wins, and the PDF stays blocked, even though you may have intended the Allow to carve out PDFs. To fix it, make the Allow longer, for example Allow: /downloads/*.pdf$ (24 characters).
Gotchas
- Case sensitivity.
Disallow: /*.pdf$does not block/Report.PDF. If your site serves mixed-case extensions, add a second rule. - Parameter order.
Disallow: /*?sort=blocks/shop?sort=pricebut not/shop?page=2&sort=price. Use/*?*sort=or add/*&sort=to cover both positions. - Trailing slashes.
/blogmatches/blog,/blog/, and/blog-archive./blog/matches only paths inside the directory, and does not match the bare URL/blog. - Blocking resources. A broad rule such as
Disallow: /*.js$can stop search engines from fetching scripts needed to render your pages. Check what a pattern catches before deploying it. - Over-matching. A short wildcard rule can block more than you expect.
Disallow: /*printblocks/blueprint-guide. Anchor with slashes where you can. - Parser differences. Major search engines support
*and$. Smaller crawlers and homemade scripts may not, and treat the characters literally.
Practice
Exercise
A robots.txt group contains these two rules. Is /shop/shoes?sort=price crawlable? Is /shop/shoes?sort=price&page=2? Explain using rule length and the $ anchor.
Disallow: /shop/?sort=
Allow: /shop/?sort=price$
Show answer
Both rules match /shop/shoes?sort=price. The Allow rule is 19 characters and the Disallow rule is 13, so the longer Allow wins and the URL is crawlable. For /shop/shoes?sort=price&page=2, the Allow rule does not match, because the $ requires the URL to end right after price. Only the Disallow rule matches, so that URL is blocked.
Exercise
You want to block all .pdf files, including ones requested with a query string such as /docs/a.pdf?v=2, but you must not block /docs/pdf-guide. Which rule should you use, Disallow: /*.pdf$ or Disallow: /*.pdf, and why does neither choice match perfectly?
Show answer
Disallow: /*.pdf blocks /docs/a.pdf?v=2, which the anchored version misses. It does not block /docs/pdf-guide, since that path has no .pdf substring. It could, however, block URLs like /docs/a.pdf-viewer. The anchored rule /*.pdf$ avoids that but lets query-string URLs through. A precise fix is to use both Disallow: /*.pdf$ and Disallow: /*.pdf?.