if (document.referrer !== "http://www.stackoverflow.com") {
window.location.href = "http://www.google.com";
}
Or you can use regular expressions to check the referrer.
Anyway, this solution is really, really unsafe. You can just turn off JavaScript in your browser and won't be redirected...