A Caddyfile for hosting your clearnet domain and all subdomains on Tor

After installing torrc and pointing your hidden service to your Caddy server:

# Redirect base domain to www subdomain
http://example.onion {
  redir http://www.example.onion{uri}
}

# Proxy all subdomains of example.com
http://*.example.onion {
  @hostnames header_regexp hostname Host (\S+)\.example\.onion
  # note the backslashes before the dots

  handle @hostnames {
    reverse_proxy {re.hostname.1}.example.com:443 {
      header_up Host {re.hostname.1}.example.com
      transport http {
        tls
      }
    }
  }
}

Replacing example.onion with whatever your onion hostname is, e.g. xoe4vn5uwdztif6goazfbmogh6wh5jc4up35bqdflu6bkdc5cas5vjqd.onion, and example.com with your clearnet domain.

For example this configuration allows:

...without configuring Tor on each server.