2

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.

Sted
  • 111
  • 6
  • You can start using parse_url https://www.php.net/manual/en/function.parse-url.php . With this function you can easily verify if your site is visited with any parameters. If so, you can simply use a `header("location......")` again without the parameters. Maybe, you can use some magic with `.htaccess` as well. But that's beyond my knowledge :) – Gowire Nov 09 '22 at 13:40
  • 1
    You could do this in js: `window.history.pushState('', '', '/')` . You can even test in in developer console at your site after fb-redirect – Techno Nov 09 '22 at 13:41
  • @Techno This works but it also removing the premalink. `example.com/page.php?something` instead of `example.com/page.php`. it displaying `example.com` – Sted Nov 09 '22 at 13:45

2 Answers2

1

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({}, '', '/');
dvicemuse
  • 370
  • 2
  • 10
  • This works but it also removing the premalink. `example.com/page.php?something` instead of `example.com/page.php`. it displaying `example.com` – Sted Nov 09 '22 at 13:46
  • then you would want window.history.pushState({}, '', 'page.php'); – dvicemuse Nov 09 '22 at 13:55
  • Then i have to place this code every time for different pages. it like `WordPress` website. I can place in header or footer and work for all the pages & post. – Sted Nov 09 '22 at 13:57
  • Then you would want to parse the path from the url https://dmitripavlutin.com/parse-url-javascript/#5-pathname – dvicemuse Nov 09 '22 at 13:59
  • 1
    you could do something like window.history.pushState({}, '', window.location.pathname); – dvicemuse Nov 09 '22 at 14:00
  • And one more question. What if i want to remove specific string from URL. not all string. How i can use your code to achieve it? – Sted Nov 09 '22 at 14:01
  • if you wanted to parse the query parameters (?name=val&name2=val2) you would use window.location.search to get everything from the url after the "?" then you would replace the text you want removed... then you would use the result as the last parameter in the window.history.pushState method – dvicemuse Nov 09 '22 at 14:06
  • if you're happy with the answer please flag it as accepted – dvicemuse Nov 09 '22 at 14:09
0

For Reference :

Answer by @dvicemuse & @Techno

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

Documents :

Sted
  • 111
  • 6