Catch-all path matching: path_regexp and file interaction

1. The problem I’m having:

I’m trying to make rewrite matcher that rewrites path /a/b/c/d to /a.php, /a/b.php, /a/b/c.php if the file exists. I know I probably can’t do this recursively for any number of subpaths, but I was hoping I could pull it off for a fixed number of them (up to 4, for example).

High level, what I’m trying to do is the expanded version of php_fastcgi, with an added automatic “catch all” semantic.

3. Caddy version:

v2.10.0-beta.4

4. What I have tried:

@subpath1 {
	path_regexp m1 ^(.*?)(/[^/]+)$
	file {re.m1.1}.php
}
rewrite @subpath1 {re.m1.1}.php

This seems to work sometimes. When I add a second @subpath2 to try to match /a/b/c with /a.php, then this subpath1 stops working. It seems very brittle.

Is there a supported way to do what I want?

If not, is there a reason why those subpaths wouldn’t work or be order dependent?

Adding some debug info to caddy, it seems that:

	   path_regexp m1 ^(.*)(/[^/]*)$
	   file {re.m1.1}.php

doesn’t guarantee that path_regexp runs before file. So file gets executed without being able to expand the matched. Is this a bug?