This regex101 demo is pretty naive but is a basic pattern that would work for most cases, and you end up with the full URL in the full match and $2
would contain the domain for use in the substitution.
I’m about to go out so don’t have time to stick it in an action, but it’s a start.