Use something like:
$headers = getallheaders();
if ($headers["Host"] != "www.mysite.com")
header("Location: http://www.mysite.com");
or something to get the HTTP/1.1 "Host" header, and then determine what URL they got that through.
Doing it in any other way would be very much impossible - were it possible, anyone can say that www.microsoft.com and www.linux.com are on the same server 😉