简体   繁体   中英

How do I make external links pass through an affiliate link first using .htaccess or Javascript?

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?

 // 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM