2

I have an URL like this:

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

And I'm trying to redirect it to:

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

I tried the following:

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

And this code too : window.location.replace(hash.replace('#/', ''));

Both of these solutions doesn't redirect as intended & the page end up in http://localhost:3000

Edit:

When I copy & paste the URL both of solutions above work! But when I click on the link( http://localhost:3000/#/contrat?id=8171675304 ) on the browser it redirects to http://localhost:3000

Melchia
  • 22,578
  • 22
  • 103
  • 117
  • Possible duplicate of [How to stop /#/ in browser with react-router?](https://stackoverflow.com/questions/25086832/how-to-stop-in-browser-with-react-router) – Jordi Nebot Jun 13 '19 at 15:15
  • @JordiNebot already tried the solution. It doesn't work in my case. I don't want to use HashRouter! – Melchia Jun 13 '19 at 15:16

2 Answers2

0

Try this please !

.replace( /#.*/, "");
user_1330
  • 504
  • 1
  • 5
  • 22
0

I had in mind something like:

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

But with your most recent edit about not working when you click, you might want a different approach for that part:

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

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

All that being said, I'm quite new to JS, so it might not be the best way to do it.