I see a few things with the above code that are likely causing some issues. First, after the || you need to add $referer =, the string is probably just equating to true and thus always making the if execute. The other thing to consider is what if they add www or some other subdomain to the beginning of the url? That will not satisfy this explicit string match. I would use strpos() to check for the substring of "mydomain2.com" for example.
A better way to handle this is really outside of PHP altogether and instead use the apache settings. Not sure what sort of settings your host provides, but you can setup virtual hosts in apache and run multiple domains that way. Another way is to use mod_rewrite in a .htaccess file and redirect based on the url in there.