如何在不重定向用户的情况下更改地址栏 URL(不使用历史记录 API?)

How to change address bar URL without redirecting the user (not using History API?)

我知道这是一个很常见的问题。但是我找不到我要找的答案。

我需要实现与 Groupon.com 非常相似的行为。例如,当您访问 https://www.groupon.com/goods/all. On the left side you will see an "Interest" filtering widget and when you select one or more option, the page URL on the address bar will change to something like https://www.groupon.com/goods/all?interest=good-for-gifting.

首先,我认为他们正在使用 History API,但他们没有。因为当我检查 history.length 看它是否增加时,它总是 returns 相同的值。

有人知道怎么做吗?

可能是因为他们使用了 replaceState() 方法,该方法不会向历史记录中添加任何条目,而是替换当前条目

https://developer.mozilla.org/en-US/docs/Web/API/History_API

为什么不试试 URL 在 htaccess 或配置文件中重写。可以参考下面的文章。 http://webrewrite.com/url-rewriting-htaccess/

希望这有用!