Skip to content

Instantly share code, notes, and snippets.

Last active November 28, 2023 20:28
Show Gist options
  • Save jdevalk/5622742 to your computer and use it in GitHub Desktop.
Save jdevalk/5622742 to your computer and use it in GitHub Desktop.
These three files together form an affiliate link redirect script.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule (.*) ./index.php?id=$1 [L]
$id = isset( $_GET['id'] ) ? rtrim( trim( $_GET['id'] ), '/' ) : 'default';
$f = fopen( 'redirects.txt', 'r' );
$urls = array();
// The file didn't open correctly.
if ( !$f ) {
echo 'Make sure you create your redirects.txt file and that it\'s readable by the redirect script.';
// Read the input file and parse it into an array
while( $data = fgetcsv( $f ) ) {
if ( !isset( $data[0] ) || !isset( $data[1] ) )
$key = trim( $data[0] );
$val = trim( $data[1] );
$urls[ $key ] = $val;
// Check if the given ID is set, if it is, set the URL to that, if not, default
$url = ( isset( $urls[ $id ] ) ) ? $urls[ $id ] : ( isset( $urls[ 'default' ] ) ? $urls[ 'default' ] : false );
if ( $url ) {
header( "X-Robots-Tag: noindex, nofollow", true );
header( "Location: " . $url, 302 );
} else {
echo '<p>Make sure yor redirects.txt file contains a default value, syntax:</p>
<p>Where you should replace with your domain.</p>';
Copy link

3mounika commented Feb 4, 2015

I have tried this on wordpress blog It's working for me.
But i have lossed what i am getting before.

i.e I am using affiliate links like this -[currentuser_id](short code plugin) i am getting at the front with current logged in user id example for admin is -

But after using this trick i have lossed it, at the front end i am getting, but clicking on that link it will going directly -

Please help on this. Please

Copy link

Thanks so much for this script!

Is it allowed for Amazon affiliate links?

Copy link

pnb1 commented May 21, 2015

I'm sorry, but where exactly I need to put these files? Do I need to append this code to the existing files?

Copy link

kenroar commented Jul 29, 2015

I hope you don't think my questions are stupid. I am not well versed at this. I need to cloak my Amazon affiliate links.

  1. Will the redirect open in a new window or do I have to add the "target=_blank" code in the URL?
  2. Do I have to create a separate script for every affiliate link in the /out/ folder?
    If I need a different redirect for every link would it look something like this?
  3. Does the redirect file in the /out/ folder act like CSS where I add something in the original URL in HTML in my pages such as div="redirect"

Copy link

ghost commented Sep 2, 2015

Hi @kenroar

  1. It will not open in a new window, but yes, if you use target="_blank" in your link tag (a href=...), it will open in a new link.
  2. No, you don't need a separate script (if you mean PHP script), the single index.php script works for all links, but as you pointed out, you need a unique entry in redirects.txt for each affiliate link.
  3. Not sure what you mean but the redirect file in /out/ is not in any way related to CSS. It simply accepts a target id through the href link in your HTML's a tag and redirects accordingly. Any CSS should be added to the HTML tag. So, an example link would be something like this assuming you use the folder /out/ as Yoast does:
<a href="/out/?id=amazon" target="_blank" class="{whatever css class you want here, nothing to do with the redirect}">Get this product on Amazon</a>

Copy link


I’m no PHP expert, but I wanted to change the 302 status code to 307. When I did it, I may have found redundant code here: header( "Location: " . $url, 302 );

First, I attempted to change the current 302 copy to 307. This did nothing. I did a bit of research and then changed the X-Robots-Tag instead:

header( "X-Robots-Tag: noindex, nofollow", true );
header( "X-Robots-Tag: noindex, nofollow", true, 307 );

I also deleted the 302 in the Location tag since it doesn’t seem to have a purpose (The default response is 302 for redirects, so a 302 status is return whether the 302 is in that tag or not):

header( "Location: " . $url, 302 );
header( "Location: " . $url );

All put together I’m now using:

if ( $url ) {
header( "X-Robots-Tag: noindex, nofollow", true, 307 );
header( "Location: " . $url );

Please correct me if there's a purpose for the 302 in the Location tag. As far as I can tell it doesn't create the status code.

Cheers! And thanks for the script.

Copy link

prof611 commented Apr 24, 2016

I just ran across the other day, and since I am somewhat of an expert on link-cloaking, I decided to try out this method myself. It works, but there are two disadvantages compared to the method I recommend, .

(1) There is quite a bit of work involved in setting it up, and the instructions are not very clear in some places,

(2) My method has been around for years, and it is superior to yours in two very important respects: (i) The affiliate ID is not shown in the address bar of the destination URL, and (ii) The destination page may be ANYWHERE - it's not necessary for the user to be redirected to the same URL that appears in the affiliate link.

Please take a look at for a 5-step procedure of how to use CloakerBuzz, and an explanation of how it works. And download a free copy to try on your own - it's easy, and it works!

Copy link

Okay, so I have godaddy website builder, and I also have absolutely no clue how to code. I downloaded the files, but where would I even put it? When I add the code to the html code thing it just makes text. I'm so confused

Copy link

Mack86 commented Aug 18, 2017

Hi Everyone,
Find this page very interesting!!
I'm new in this cloaking stuff finally reach here.........
Can Someone tell me how to cloak it, I have watch lots of YouTube tutorial but nothing help me..

  1. index.php
  2. .htaccess
  3. redirects.
    In INDEX

Make sure yor redirects.txt file contains a default value, syntax:


Where you should replace with your domain.

'; } If my landing page is "" where should I change it and the safe page is "" where should I put it Thirdly? which code shoud I use in my index.php for Highly appreciate!!

Copy link

What I did is just upload 3 files into my domain main directory and overwrite .htaccess and replace my domain in redirects.txt and I am getting following error ERR_TOO_MANY_REDIRECTS in chrome browser I didn't create any robots.txt. when redirects.txt was default my domain redirects to
Will someone please help me to sort this issue?

Copy link

MomBot commented Apr 26, 2018

It does what it says on the tin! I had already created a subdomain for my website, which I won't list here, but I simply created a subdomain and named it "go" and then issued a Let's Encrypt SSL certificate for it (because... why not). I created a robots.txt file and yes I did exclude that whole subdomain. Then I uploaded the three files (index.php, .htaccess, and redirects.txt). In the redirects.txt file, I started with my default entry, which is my homepage, and then listed out all my affiliate links. I keep a separate spreadsheet with the raw and cloaked links for all affiliate programs. When I tested the links, they simply worked! I'll now be setting up this same list on all my sites so that they can individually link to the affiliate links smoothly. This rocks! Thank you, Yoast and Github!

Someone advised me to cloak all my outbound links for consistency and because if those links start up an affiliate program later, it's simple to change the entry for that link in the redirects.txt file and then it's done! I also have a couple of affiliate programs that I'm waiting on an approval for. I'll go ahead and write my articles and link to the regular site with the cloaked link, then once the programs are set up, I'll insert my new affiliate link into the redirects.txt file.

One last note: the question marks in my links did not require special handling as someone noted above. I left them in place and the links worked as desired. Color me one happy camper!

Copy link

This is so fcking incredibly useful! Thanks a lot.

Copy link

zakul commented Apr 21, 2020

Hi, great work on this solution!
I am wondering if it is possible to have Google Analytics for the links which will be then redirected?!
Does anyone have a solution for this or can point me in the right direction?

Copy link

Hi, great work on this solution!
I am wondering if it is possible to have Google Analytics for the links which will be then redirected?!
Does anyone have a solution for this or can point me in the right direction?

My solution is to use Google tag manager to set a "goal" to capture click url that contains "go" (just treat it as an external link), otherwise Google analytic alone could not capture it because you have no Google Analytic code in the /go url. Correct me if I am wrong.

Copy link

Johndale99 commented May 18, 2021

Great work. Any idea how I block direct browser access to the redirects.txt file via htaccess?

Currently all my aff urls are accessible via https://my-site/link/redirects.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment