Home > Back-end >  Remove string from URL when site is loading
Remove string from URL when site is loading

Time:11-10

I want to remove specific or full parameters from the URL of the website when it is actively loading in the browser. I want to do this because some website including additional strings.

https://www.example.com/?gclid=anything
https://www.example.com/?fbclid=anything
https://www.example.com/?msclid=anything

These are the tokens sent by third party like Google, Facebook, etc. I want to remove that.

For example, if peoples click my link on facebook https://www.example.com/ than Facebook will include https://www.example.com/?fbclid=something but i want ?fbclid=something should be removed and peoples land to https://www.example.com/ instead of https://www.example.com/?fbclid=something

My code :

$url = strtok($_SERVER["REQUEST_URI"], '?');

I have already checked Strip off URL parameter with PHP & How to remove the querystring and get only the URL? but no success.

Please suggest me how to achieve this using PHP or JavaScript.

CodePudding user response:

What you're looking for is history.pushState

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

The below example will change the url https://www.example.com/?fbclid=something to https://www.example.com/

window.history.pushState({}, '', '/');

CodePudding user response:

For Reference :

Answer by @dvicemuse & @Techno

<script>
//window.history.pushState('', '', '/');
//window.history.pushState({}, '', '/'); 
window.history.pushState({}, '', window.location.pathname);
</script>

Documents :

  • Related