Adding an Affiliate Link Redirect Script Built Into Every WordPress Post

Affiliate Link Redirect ScriptThis is a very simple yet effective way to make every post of on your Website have the ability to mask your affiliate links. This solution works perfectly for Websites that have about 50 or less affiliate links to manage, which most Blogs fit into. 5E7BWGX7BRQP

Why Mask Affiliate Links?

Masking your affiliate links has several benefits, but the key benefits are:

  • A shortened affiliate link that you create is easier for you to remember.
  • The redirect script makes affiliate links look pretty.
  • It makes links look like they are internal, resulting in more clicks.
  • A visitor is not able to copy/paste the link while removing your affiliate id.
  • Reduces 3rd party software blocking, such as Norton blocking affiliate links.
 

 

Affiliate Link Redirect Script For WordPress Posts

  • Click the ‘view plain’ link below to open the example; copy and paste the example into your favorite editor (notepad2, etc) on your computer.
  • Save and Name the file: redirect.php
<?php /** Redirect Script */
switch ($_GET['l']) {
/** ============================================= Affiliate Link Redirects == */
/** Duplicate Here - START */
case "aweber":
	$url = "http://www.aweber.com/?403565";
	header("HTTP/1.1 301 Moved Permanently");
	header("Location: $url");
	break;
/** Duplicate Here - END */
} /* Do Not Remove */?>

How The Link Redirect Script Works

The redirect script uses the PHP switch statement, which uses a case statement, and together they work like an if statement in PHP. (If something is true, do the redirect.)

The switch statement looks for the variable being passed in, with the redirect script the variable passed in is: L or l this could be anything really but I use L because it makes the URL to the script more compact.

Each 'case' statement works like an if statement. In the above example, the case statement works like: if the variable $l equals aweber then redirect to the provided url.

Typically when you use a case statements you set one of the cases as default. But because this script is included into every blog post, we exclude the default case statement. If the script had a default case, then if no value is passed in the script or if the value was incorrect, the default case would activate.

Customize The Redirect Script

Affiliate Link Redirect Script ExampleNext you will need to modify the affiliate link redirect script. To do this you duplicate 'case' portion of the script, between the /** Duplicate Here */ Comments.

  • Modify (or duplicate) the first case statement.
  • Change the case statement name: case="aweber" to whatever you would like your case to be named. It could be a number, a different word, or random text/numbers, just about anything really.
  • Modify the URL that you want to redirect to: A proper affiliate link, a different blog, etc.
  • Once done, FTP to your active theme directory for your Website and upload the redirect.php file.
/** Duplicate Here - START */
case "aweber":
	$url = "http://www.aweber.com/?403565";
	header("HTTP/1.1 301 Moved Permanently");
	header("Location: $url");
	break;
/** Duplicate Here - END */
} /* Do Not Remove */

Your Themes Single.php File

Include The Redirect ScriptThis could also be added to your themes header.php file (above all other php functions), transforming every page of your WordPress Website into a potential affiliate redirect location. If that is a bit much, you can also include it into your themes index.php, home.php, page.php or any post template file.

 
  • Open your theme single.php file.
  • Include the redirect.php script above the get_header(); function.
  • Save and re-upload the single.php file.
 
include("redirect.php");

Some hosts may require that you use the full path to the redirect.php file.

include("/full/server/path/to/wp-content/themes/theme-name/redirect.php");

Always Test The Redirects

You should always test your redirect to make sure you entered it correctly. The redirect URL will be based on your Permalink Structure.

Redirect URL

Examples:
http://domain.com/post-name?l=case-name
http://domain.com/00_post-name.html?l=case-name
http://domain.com/2012/12/31/post-name?l=case-name

Affiliate Link Jump Script In A Standalone WordPress Template

Instead of adding the redirect script to every post, you can add it to a custom template instead, which allows for a dedicated page to handle all redirects instead of all posts or pages.

The one major difference with this script and the one above is the first case statement is set as default. All errors and incorrect links will redirect to the default url.

  • Template Name: At the top of the file, commented out, is the Template Name: Redirect. This makes WordPress display the template within the WordPress admin. When adding a New Page, under the Page Attributes widget, within the Templates dropdown, the template Redirect will appear.
  • require('./wp-blog-header.php'); This is what makes the template actually work within WordPress. Without this the template will not function correctly.
  • When creating a new page, under the Page Attributes widget, within the Templates dropdown, select the template: Redirect
  • Publish the page
  • Create a test link, example: http://domain.com/page-name?l=case-name
<?php
/**
 * Template Name: Redirect
 * File: redirect.php
 * By: techNerdia.com
 */
require('./wp-blog-header.php'); /** Required */
/** Redirect Script */
switch ($_GET['l']) {
/** Default */
case "":
	default:
	$url = "https://technerdia.com/"; /** Modify Default Error URL */
	header("HTTP/1.1 301 Moved Permanently");
	header("Location: $url");
	break;
/** ============================================= Affiliate Link Redirects == */
/** Duplicate Here - START */
case "aweber": /**
	$url = "http://www.aweber.com/?403565";
	header("HTTP/1.1 301 Moved Permanently");
	header("Location: $url");
	break;
/** Duplicate Here - END */
} /* Do Not Touch */?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="googlebot" content="noindex,noarchive,follow,noodp" />
<meta name="robots" content="all,noindex,noarchive,follow" />
<meta name="msnbot" content="all,noindex,noarchive,follow" />
</head>
<body>
	<p style="font-size:18px;font-weight:bold;margin:50px 0 0 0;text-align:center;">An Unknown Error Has Taken Place - <a href="javascript:history.go(-1)">Go Back</a> or <a href="https://technerdia.com/">Return Home</a></p>
</body>
</html>

If you use the above solution, you should remove the page from your Sitemaps (Search Engine Sitemap and your Websites Visual Sitemap) and block the page in your robots.txt file.