Bulk redirects of multiple domain names

The goal of this topic is to identify which options are available to achieve the desired result.

The desired result: I have many domain names, all of which are to be forwarded to a subdomain of one of them.

For example:

In addition, I want all subdomains forwarded, except the target, so:

In pseudo-code:

target = "www.example.com"
if host != target:
    301 "https://" + subdomains + target + path + "?" + query

I understand that there may be several available options for achieving redirects, but I’m not sure which of them can satisfy this level of wildcarding.

Candidate options I am considering:

Which, if any, of these options can be configured to forward based on a wildcard host match?