基于域名的JavaScript重定向

web*_*x l 2 javascript dns redirect

我不是在寻找简单的重定向.

我想要做的就是这个.

人员A加载站点BOB.com并单击指向页面X的链接.
人员B加载站点TIM.com并单击指向同一页面X的链接.

页面X上有一个javascript命令,如果用户来自站点Bob.com,则重定向到Bob.com/hello.
如果用户来自TIM.com,则重定向到Tim.com/hello.
如果用户没有来自以太,那么重定向到Frank.com/opps.

此页面X将处理多个域的404错误,因此它只需要查看域名".com".它应该忽略".com"之后的所有内容.

这是我开始使用的脚本.

<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;
}
</script> 
Run Code Online (Sandbox Code Playgroud)

Viv*_*ath 7

使用 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";
}
Run Code Online (Sandbox Code Playgroud)

而不是正则表达式,你可以indexOf像你最初那样使用,但这也会匹配thisisthewrongbob.comthisisthewrongtim.com; 正则表达式更强大.