Writing several addresses separated by commas at the top of a site block, such as example.com, www.example.com, creates one block whose directives apply to every listed address. Caddy obtains a certificate covering each name and routes all of them through the same handlers.
That is convenient and it is also why a redirect from the www form to the apex cannot be written inside the same block without a host matcher: the block handles both, so an unconditional redir produces a loop. Use a separate block for the name being redirected, or a matcher such as @www host www.example.com and redir @www https://example.com{uri} permanent.
Address lines accept a scheme, host, port, and path, and a path in the address acts as an implicit path matcher for the whole block. Two blocks with overlapping addresses are an error at load time rather than a precedence rule. Snippets, defined as a name in parentheses and pulled in with import, are the way to share directives across blocks without repeating them, and they accept positional placeholders so one snippet can be parameterized per site.