There are times when you are interested in changing the domain of a website, for different reasons, and you are also interested in the same content in the new domain, you need to make a redirection with dynamic rules.
If so, use the following code, saving it in the root of your old website with the name .htaccess.
Of course you will have to change https://www.domain.com for your new website, taking special care if you have put it https and with or without www.
Finally, perform the check yourself and also use the tool http://www.redirect-checker.org/ to finish checking everything.
This code is only compatible for Linux servers, which are most of them.