I am not looking for a simple redirect.

What I am trying to do is this.

Person A loads site BOB.com and clicks a link to page X.
Person B loads site TIM.com and clicks a link to the same page X.

Page X has a javascript command on it that says, If user came from site Bob.com then redirect to Bob.com/hello.
If user came from TIM.com then redirect to Tim.com/hello.
If user didnt come from ether then redirect to Frank.com/opps.

This page X is going to handle 404 errors for multiple domains so it will need to ONLY look at the domain name upto ".com". It should ignore everything past the ".com".

This is the script I started with.

<script type='text/javascript'>
var d = new String(window.location.host);
var p = new String(window.location.pathname);
var u = "http://" + d + p;
if ((u.indexOf("bob.com") == -1) && (u.indexOf("tim.com") == -1))
u = u.replace(location.host,"bob.com/hello");
window.location = u;


Ok, i should mention I am not much of a JavaScript coder. :) any help beyond just telling me use document.referrer would be a big help lol

Written by webmaster alex l

Accepted Answer

Use document.referrer

if(/http:\/\/(www\.)?bob\.com/.test(document.referrer)) {
   window.location = "http://bob.com/hello";

else if(/http:\/\/(www\.)?tim\.com/.test(document.referrer)) {
   window.location = "http://tim.com/hello";

else {
   window.location = "http://frank.com/oops";

Instead of the regex, you can use indexOf like you did initially, but that would also match thisisthewrongbob.com and thisisthewrongtim.com; the regex is more robust.

Written by Vivin Paliath
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki