Removing Affiliate Data From Browser's URL
Often, affiliate link URLs contain parameter information. It's how the destination page knows which affiliate provided the link that was clicked.
This article is for those of you who use affiliates for at least some of the products you sell.
A link with a parameter can appear to be an affiliate link. There are people who avoid tapping on affiliate links — or resist buying if they realize they were "tricked" into tapping.
Before we continue, let me define something. Most likely, you know what a url parameter is. For those who don't, a parameter is a "?" character and other text appended to a regular URL.
As an illustration, here is a URL without a parameter and one with a parameter.
https://example.com/page.php https://example.com/page.php?name=Will
What seems not to be widely known is that the parameters can be removed from the URL in the browser's address bar.
This is the code.
<script type="text/javascript"> history.replaceState(null,'',document.URL.replace(/\?.*$/,'')); </script>
Pop that into your web page and browsers arriving with a URL parameter will have the parameter removed after they arrive.
The JavaScript to remove the URL parameter from the browser's address bar won't affect PHP. You see, PHP runs before the page loads. By the time the page is loading, the PHP will have obtained the information it needs from the URL parameter.
If you have JavaScript that also uses the URL parameter (JavaScript and URL Parameters is an example), then put the above parameter-removing JavaScript somewhere below that.
Other than the condition mentioned in the previous paragraph, parameter-removing JavaScript can be placed anywhere on the page. If you are uncertain, put the URL parameter-removing JavaScript at the bottom of the page. Even below the closing </html>
code will work.
Now you know how to change the URL in the browser's address bar. It is malleable with JavaScript. It can be changed without reloading the page.
If affiliates link to your product web site, it may comfort potential buyers if they don't see a link that appears to be an affiliate link.
(This content first appeared in Possibilities newsletter.)
Will Bontrager