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

2
  • Possible duplicate of How to stop /#/ in browser with react-router? Commented Jun 13, 2019 at 15:15
  • @JordiNebot already tried the solution. It doesn't work in my case. I don't want to use HashRouter! Commented Jun 13, 2019 at 15:16

2 Answers 2

0

Try this please !

.replace( /#.*/, "");
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.