Redirect website to another website

I want to redirect my old website to new website. I want to do this for main domain only, not sub pages.
For Example:
I have a website “example1.com”. I want to redirect “example1.com” to “example2.com”.
But if a visitor visits “example1.com/page”, he must not redirect. He should see the content on “example1.com/page”. Is this possible?

And 1 more thing. I get this error when someone visits a page that is not available. I want to redirect error page to home page.

Just put this in /index.php of example1.com

<?php header("location: http://example2.com"); ?>

https://tinkertechlab.com/webhosting/htaccess/set-error-pages

I want to do this with .htaccess.

I don’t think that’s possible without redirecting all pages

Not possible.

You need to create a rule to override the redirect rule, but the only way to override it is to create another redirect.

Writing it just by heart (haven’t tested this), you should be able to redirect a specific URL using a .htaccess like like this:

RedirectMatch "^/$" "http://example2.com/"

The standard Redirect statement does prefix matching, whereas you can do exact matching with RedirectMatch, which allows for some other redirect options.