Two Ways to Cloak Affiliate Links with a PHP File

Cloaking affiliate links are important. Let’s face it, affiliate links are ugly. They scream SPAM. People hesitate to click when they see an ugly affiliate link. But fortunately, there are quite a few ways to cloak affiliate links. There are both paid and free ways. This post will show you how to cloak your affiliate links for FREE using a simple php script.

I have previously listed few WordPress plugins that can be used to cloak affiliate links. While it’s a great way, it can sometimes slow down your site. Redirection itself can become slow because it is handled by WordPress. Using independent php files are faster and secure.

Concept

Concept is very simple. We’ll create a new directory inside our site’s document root. And we’ll create php files inside this directory that redirects to affiliate links. We can simply link to these php files (Or file) whenever we want to post affiliate links. You can see this in action on my recommended VPS providers post. We will then tell Google and other search engines not to index this directory using our robots.txt file.

Start by creating a sub directory on your server. I’m using directory /go/, you can use any directory you want. But it’s a good idea to keep it short and simple. Now you can use one of below methods to create a PHP file with redirections.

Method #1 – Single Redirection per PHP file

This is the easiest method. Just copy paste following code to your favorite text editor.

<?php
   header("Location: http://example.com/");
   exit;
?>

Change example.com with your affiliate link. Save the file as a text file. You can name it whatever you want. For example, if you’re promoting a BMW car, you can name it bmw.txt. Upload it the directory you created earlier. Rename the file to bmw.php.

Assuming that your domain is mydomain.com and you’ve named the file as bmw.php and uploaded to /go/ directory, linking to following URL will redirect user to example.com.

http://mydomain.com/go/bmw.php

Method #2 – Multiple Redirections per PHP file

Rather than having a separate php file for each affiliate link like above, I’m using this php jump script to categorize my affiliate links. So to promote VPS providers, I created vps.php file and added all my affiliate links. It gives me unique link to each provider which I can post anywhere on my site and anywhere on the net. Perry cool isn’t it?

Simply copy and paste following code to a text editor on your PC.

<?php
$v = $_GET['v'];
// In case someone calls naked php file
if ($v == '') {$link = 'http://example.com/';}

// Add as many as links in below format. Each in a new line.
if ($v == 'product1') {$link = 'http://example1.com/';}
if ($v == 'product2') {$link = 'http://example2.com/';}
if ($v == 'product3') {$link = 'http://example3.com/';}

// Don't change anything below this line.
header("Location: $link") ;
exit();
?>

Now replace product1, product2 and product3 with product names. These will be used to call the respective links. example1.com, example2.com and example3.com should also be replaced with affiliate links.

Also change the example.com in the line #4. This is the link that the user will be redirected to if they don’t have a variable specified in the URL. I usually add link to the post on my blog.

Save the file as a text file on your PC. You can use any name. I used vps.txt for my VPS providers file. Upload the file to the newly created directory in your server and rename it to .php extension. I’ve changed mine to vps.php.

Assuming that your domain is mydomain.com and you’ve named the file as cars.php and uploaded to /go/ directory, linking to following URL will redirect user to example1.com.

http://mydomain.com/go/cars.php?v=product1

And linking to following URL will redirect user to example2.com.

http://mydomain.com/go/cars.php?v=product2

Block Search Engines from Crawling and Indexing PHP files

We don’t want Google to crawl these redirections. We can easily prevent it and other bots from crawling these files by blocking access to the directory. So open your robots.txt file and add following code to it.

User-agent: *
disallow: /go/*

If you don’t have a robots.txt file on your server, create a new one and add above lines. It should take care of it. You can use Google Search Console to verify if rules are updated.

Tharindu

Hey!! I'm Tharindu. I'm from Sri Lanka. I'm a part time freelancer and this is my blog where I write about everything I think might be useful to readers. If you read a tutorial here and want to hire me, contact me here.

10 Comments

  1. I have a question about #1 method: what if I don’t want it to redirect to the link but just load the page in the background(without actually redirect)

  2. HI, it doesn’t work for me, after opening url it just open php code as above, not redirected. Don’t you know what might be the issue?

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button