Skip to content

Instantly share code, notes, and snippets.

@devnepsys
Forked from jdevalk/.htaccess
Created July 21, 2021 09:43
Show Gist options
  • Select an option

  • Save devnepsys/789b2e8760fa18b04ff15e5b6e1fbc67 to your computer and use it in GitHub Desktop.

Select an option

Save devnepsys/789b2e8760fa18b04ff15e5b6e1fbc67 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'] ) ? $_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 ) ) {
$urls[ $data[0] ] = $data[1];
}
// 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' ] : '' );
if ( isset( $url ) ) {
header( "X-Robots-Tag: noindex, nofollow", true );
header( "Location: " . $url, 302 );
die;
}
default,http://example.com
yoast,http://yoast.com
test,http://yoast.com/?p=2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment