wordpress – How do I redirect pages to the subdomain with the same URL format?

How do I redirect the pages of the main site with status 404 to the subdomain with the same URL structure?

This for example:

https://www.example.com/rent

this returns a 404 error and must be redirected to:

http://subdomain.example.com/rent

Is this possible in WordPress? If it is how we can do it?