Are you using Cloudflare?
Can you use .htaccess redirects?
This should preserve the path - just swap the domain:
Code:
# Enable the Rewrite Engine
RewriteEngine On
# Condition: If the request is NOT for the root domain
RewriteCond %{REQUEST_URI} !^/$
# Redirect all requests while preserving the path
RewriteRule ^(.*)$ https://domain2.com/$1 [R=302,L]
This should redirect straight to root:
Code:
# Enable the Rewrite Engine
RewriteEngine On
# Condition: If the request is NOT for the root domain
RewriteCond %{REQUEST_URI} !^/$
# Redirect all requests without preserving the path
RewriteRule ^(.*)$ https://domain2.com/ [R=302,L]
Change "302" to "301" once you confirm it works - if you wish to keep it permanent.
For anyone reading this in the future: those are intended for .htacces.
I haven't played much with Cloudflare "Redirects rules" until today (they are sunsetting the "Page rules" which I used to use). Maybe it can be done via CF too, before any request even reaches the hosting server - if CF is your cup of tea. But this .htaccess should work, and quite efficiently too.