【问题标题】:Redirect not working as expected重定向未按预期工作
【发布时间】:2015-11-04 10:39:02
【问题描述】:

它应该如下工作:如果url匹配域然后重定向到新域+添加url路径+查询字符串:

if (window.location.href == "www.heelsteal.com.au") {
    var pathName = window.location.pathname 
   window.location = ("www.wittner.com.au/" + pathName + "?string=redirect"); 
}

我得到的只是undefined

【问题讨论】:

标签: javascript redirect seo


【解决方案1】:

您可以尝试使用indexOf()

indexOf() 如果未找到字符串,则返回 -1,如果找到字符串,则返回 0

window.location.origin 将返回域名,因此我们可以在if 条件中使用它。

window.location.pathname 您已经知道将返回路径,但如果不存在路径,它将返回 /,因此我已将其从重定向 URL 中删除,否则您将获得 www.wittner.com.au// somepath?string=redirect 而不是 www.wittner.com.au/somepath?string=redirect

if(window.location.origin.indexOf('heelsteal.com.au')>-1){
window.location.href='http://www.wittner.com.au'+window.location.pathname+'?string=redirect';
}

如果您对以上源代码有任何疑问,请在下方留言,我会尽快回复您。

我希望这会有所帮助,祝您编码愉快!

【讨论】:

    猜你喜欢
    • 2015-03-15
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多