Last active
January 7, 2025 07:26
-
Star
(106)
You must be signed in to star a gist -
Fork
(29)
You must be signed in to fork a gist
-
-
Save jdevalk/5622742 to your computer and use it in GitHub Desktop.
These three files together form an affiliate link redirect script.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<IfModule mod_rewrite.c> | |
RewriteEngine On | |
RewriteRule ^index\.php$ - [L] | |
RewriteRule (.*) ./index.php?id=$1 [L] | |
</IfModule> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
default,http://example.com | |
yoast,http://yoast.com | |
test,http://yoast.com/?p=2 |
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.
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
can you convert htaccess to nginx?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?