How do * and $ work in robots.txt?

By TechCompare · Updated · How we check

Use * for any text and $ to anchor the end. A pattern without them is a prefix, and matching is case sensitive.

Only two characters are special in a robots.txt path. The * matches any run of characters, including none, and $ means the URL ends here. Everything else matches itself. Every pattern is also a prefix match from the start of the path, which is why Disallow: /private blocks more than people expect.

Topic
Wildcards and $
Pattern matching
Category
Reading the rules
Precedence, wildcards and size

How it works

Google's examples show where patterns land. Disallow: /*.pdf$ blocks /files/a.pdf but not /files/a.pdf?dl=1, because the query string is part of what's matched and the URL no longer ends in .pdf. Drop the $ and both are blocked. Disallow: /*?sort= blocks /list?sort=price. Disallow: /private blocks /private-notes and /private/x alike, while Disallow: /private/ only blocks the folder. Google says a trailing wildcard is ignored, so /fish* is the same as /fish. Matching is case sensitive, so Disallow: /Photos/ doesn't block /photos/a. RFC 9309 defines only * and $, so regular expression syntax like (jpg|png) isn't part of the language. The tester shows the pattern that matched, so you can see whether the rule you meant is the one that fired. When a pattern matches nothing, check the case and the slash first.

Verdict

Start from the URLs you want blocked and write the shortest pattern that catches them, with $ when the end matters. Then test one URL that should match and two near misses, like the same path in another case and with a query string. The near misses are where patterns go wrong.

More Robots.txt scenarios

Block GPTBot
Two lines tell OpenAI's training crawler to stay out. Its search crawler and its page visits use other names.
View details
Google-Extended
A token for Gemini training, not a crawler. A rule for it never reaches Googlebot or Google Search.
View details
Block AI, keep search
Name each training crawler, leave Googlebot and Bingbot alone, and choose on purpose about the AI search bots.
View details

Frequently asked questions

Does Disallow: /*.pdf$ block links with a query string?
No. The query string is part of the path, so /a.pdf?dl=1 doesn't end in .pdf. Google's own examples show the same thing for /*.php$. Remove the $ to block both.
Is robots.txt case sensitive?
Paths are. Disallow: /Photos/ doesn't block /photos/. Field names and user agent names are not case sensitive.
Can I use a regular expression?
No. The standard defines the * and $ characters and nothing else, so a pattern like /*.(jpg|png)$ doesn't do what it looks like. Write one rule per file type instead.