Skip to content

Instantly share code, notes, and snippets.

@mlent
Forked from TedAvery/.nginxconfig
Last active February 7, 2023 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlent/f5e0407d1036f319972779ba2d43e0a5 to your computer and use it in GitHub Desktop.
Save mlent/f5e0407d1036f319972779ba2d43e0a5 to your computer and use it in GitHub Desktop.
Yoast affiliate redirect for Affilimate
location /go/ {
rewrite ^/go/(.*)$ /go/index.php?id=$1 last;
}
<?php
$id = isset( $_GET['id'] ) ? rtrim( trim( $_GET['id'] ), '/' ) : 'default';
$params = $_SERVER['QUERY_STRING'];
$pos = strpos($params, "&");
$args = $pos !== FALSE ? substr($params, $pos + 1) : '';
$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 );
// check for querystring
if ($args) {
// if this is a Partnerize link, append as URL path, otherwise pass onto link
// regex test: https://www.phpliveregex.com/p/EVI
preg_match('/https:\/\/prf.hn\/click\/camref:(\w*)/', $url, $ph_url_match);
if ($ph_url_match) {
$url = $ph_url_match[0].'/'.str_replace('=',':',$args).preg_split('/https:\/\/prf.hn\/click\/camref:(\w*)/', $url)[1];
} else {
$url .= ( strpos($url, '?') ? '&' : '?' ).$args;
}
}
if ( $url ) {
header( "X-Robots-Tag: noindex, nofollow", true );
header( "Location: " . $url, 302 );
die;
} else {
echo '<p>Make sure your 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>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment