我不是在寻找简单的重定向。
我想做的就是这个。
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>
最佳答案
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";
}
您可以像最初一样使用 indexOf
而不是正则表达式,但这也将匹配 thisisthewrongbob.com
和 thisisthewrongtim.com
;正则表达式更加强大。
关于JavaScript基于域名的重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4925528/