【问题标题】:Redirect from home url to url+anchor从主页 url 重定向到 url+anchor
【发布时间】:2014-10-03 11:25:43
【问题描述】:

如何将进入网站 example.com 的访问者重定向到 example.com#start ? 但前提是它是 example.com 而不是 example.com#about 或 example.com#contact

我尝试过使用

window.location.hash="start"

但是当我输入 example.com#contact 时,它会重定向到 example.com#start

我也试过了

    if(window.location.hash != ''){ 
    window.location.hash="start";
}

但这根本不起作用。

我不知道解决方案应该是 javascript、php 还是 htaccess。有谁知道这个的顺利解决方案?

【问题讨论】:

    标签: javascript php .htaccess anchor url-redirection


    【解决方案1】:

    您的代码检查哈希是否不等于 '',如果设置了哈希,则返回 true。因此它总是会重定向到开始。

    您应该使用以下内容:

    if(window.location.hash == ''){ 
        window.location.hash="start";
    }
    

    【讨论】:

    • 没问题。经过漫长的编码日,我们都会犯这样的错误!
    【解决方案2】:

    使用 window.location.href="www.mypage.com";

    您使用的哈希只是在 url 后添加一个哈希符号,即:www.mypage.com# window.location.href 将定位到新页面或 url。

    【讨论】:

      【解决方案3】:

      我想知道您为什么要使用哈希进行重定向,但这当然不关我的事。 ^^

      您的第二个解决方案有些正确,但您使用了 '!=' 而应该是 '=='。 如果我理解正确,如果没有主题标签,您希望将页面重定向到 example.com#start。 这应该有效:

      if(window.location.hash == '')
      {
          window.location.hash = "start";
          //If it doesn't work, try adding this here: window.location.reload();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-28
        • 2020-07-24
        • 2017-01-29
        • 2017-05-29
        • 2015-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多