从主页url重定向到url+锚点


Redirect from home url to url+anchor

如何将进入网站example.com的访问者重定向到example.com#start?但只有当它是example.com而不是example.com#about或example.com#时,请联系

我试过使用

window.location.hash="start"

但当我键入example.com#contact时,它会重定向到example.com#启动

我也试过

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

但这根本不起作用。

我不知道解决方案应该是javascript、php还是htaccess。有人知道一个平滑的解决方案吗?

您的代码检查hash是否不等于",如果设置了hash,则返回true。因此,它将始终重定向到启动。

您应该使用以下内容:

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

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

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

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

您的第二个解决方案在某种程度上是正确的,但您使用了"!="应该是"=="的时候。如果我理解正确的话,如果没有标签,你想将页面重定向到example.com#start。这应该有效:

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