Rewrite URL from one domain to another

I need to completely rewrite domain1.com to replace the URL *domain2.com and I haven’t been able to figure this out yet. The Apache example I’ve been given is this:

RewriteCond %{HTTP_HOST} ^sub.domain1.com$ [NC]
RewriteRule ^/(.*)$ https://sub.domain2.com/$1 [P,L]

I really need to have a path on domain2.com as well.
RewriteCond %{HTTP_HOST} ^sub.domain1.com$ [NC]
RewriteRule ^/(.*)$ https://sub.domain2.com/pathtoapp/$1 [P,L]

Anyone have any ideas?

not tested, but should be something like

http-request set-var(txn.host_header) req.hdr(Host),lower
acl is_domain1 var(txn.host_header) -m beg sub.domain1.com
http-request set-header Host sub.domain2.com if is_domain1
http-request set-uri  /pathtoapp/%[capture.req.uri] if is_domain1

See https://cbonte.github.io/haproxy-dconv/1.7/configuration.html#4.2-http-request

Good luck

Sascha