将哈希 URL 重定向到标准 URL

Posted

技术标签:

【中文标题】将哈希 URL 重定向到标准 URL【英文标题】:Redirect Hash URL to standard URL 【发布时间】:2019-10-28 05:31:01 【问题描述】:

我有一个这样的网址:

http://localhost:3000/#/contrat?id=8171675304 

我正在尝试将其重定向到:

http://localhost:3000/contrat?id=8171675304

我尝试了以下方法:

window.location.assign(hash.replace('#/', ''));

还有这段代码:window.location.replace(hash.replace('#/', ''));

这两种解决方案都没有按预期重定向,页面最终位于http://localhost:3000

编辑:

当我复制并粘贴 URL 时,上述两种解决方案都有效!但是当我点击浏览器上的链接(http://localhost:3000/#/contrat?id=8171675304)时,它会重定向到http://localhost:3000

【问题讨论】:

How to stop /#/ in browser with react-router?的可能重复 @JordiNebot 已经尝试过解决方案。在我的情况下它不起作用。我不想使用 HashRouter! 【参考方案1】:

请试试这个!

.replace( /#.*/, "");

【讨论】:

【参考方案2】:

我想到的是这样的:

window.location.replace(window.location.href.replace('#/',''))

但您最近编辑的内容是单击时不起作用,您可能需要 a different approach 来处理该部分:

// Simulate a mouse click:
window.location.href = "http://www.w3schools.com";

// Simulate an HTTP redirect:
window.location.replace("http://www.w3schools.com");

话虽如此,我对 JS 还是很陌生,所以这可能不是最好的方法。

【讨论】:

以上是关于将哈希 URL 重定向到标准 URL的主要内容,如果未能解决你的问题,请参考以下文章