-1

I have a significant amount of external links on my website in this format:

website.com/product/[variable]

I need these links to somehow pass through “myaffiliatelink.com” before being redirected to website.com/product/[variable].

Is this possible using .htaccess or Javascript?

I looked into using .htaccess, but seems I would need to do this individually. Is there a way to set a rule such that any external link using "website.com/product/[variable]" should pass through "myaffiliatelink.com" first?

1 Answers1

-1

// catch every click on the page
document.addEventListener("click", e => {
  const target = e.target;
  if (target.tagName === 'A' && target.href.indexOf("website.com") !== -1) {
    // prevent the <a> tag from navigating
    e.preventDefault();
    const lastSlash = target.href.lastIndexOf('/');
    if (lastSlash > 0) {
      const variable = target.href.substring(lastSlash + 1);
      // use the commented code below, or a window.open
      //location.href = "https://myaffiliatelink.com/"+variable;
      // for demonstration
      console.log("https://myaffiliatelink.com/" + variable);
    }
  }
})
<a href="https://website.com/product/[variable]">Product</a>
<p>should pass through "myaffiliatelink.com"</p>
user2182349
  • 9,569
  • 3
  • 29
  • 41