Skip to content

Instantly share code, notes, and snippets.

@jdevalk
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]
</IfModule>
<?php
$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.';
die;
}
// Read the input file and parse it into an array
while( $data = fgetcsv( $f ) ) {
if ( !isset( $data[0] ) || !isset( $data[1] ) )
continue;
$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 );
die;
} else {
echo '<p>Make sure yor redirects.txt file contains a default value, syntax:</p>
<pre>default,http://example.com</pre>
<p>Where you should replace example.com with your domain.</p>';
}
default,http://example.com
yoast,http://yoast.com
test,http://yoast.com/?p=2
@MomBot
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!

@ssoulless
Copy link

This is so fcking incredibly useful! Thanks a lot.

@zakul
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?

@upsangel
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.

@Johndale99
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