Recursive matcher - how to?

The docs are sadly silent on how to write a recursive matcher in Caddyfile.

For example, let’s say I want to return a 404 for any /.git/ directory anywhere.

At the moment I have the following syntax:

error /.git/* 404
error /*/.git/* 404

But this does not work recursively, i.e. /test/test1/.git/test.txt will still display. :frowning_face:

I also tried error */.git/* 404 but that just returns 404 for everything.

In this case, you’d actually want to use the hide option of file_server. It’s designed to do exactly this. There’s even an example that covers this exact thing!

But if you must use a matcher, you’d need to use path_regexp for this. The path matcher (which is what you get when you use a matcher token that starts with /, as explained in the Syntax section) doesn’t support matching across path segment boundaries with *.

It might look like this:

@errorGit path_regexp .*/\.git.*
error @errorGit 404

See the regexp test I wrote for this (click the Unit Tests tab on the left)


I’d never heard of hide hence my attempts at matchers.

So thank you, not for one answer but two !


This topic was automatically closed after 30 days. New replies are no longer allowed.