'Redirect mechanism that only replaces the domain name and keeps the path

There is a website I used to visit called www.example.com. This website had its content under various paths such as www.example.com/abc123.

Now, there is a new website that provides the same content: www.newwebsite.com/abc123. As you see, this new one uses the same path, just replacing the domain name.

Is it possible to create a mechanism, so that when I visit www.example.com/abc123, it redirects me to www.newwebsite.com/abc123 instead?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source