How to mask affiliate links from a datafeed

One of the more common affiliate marketing tips is that you should mask your outbound affiliate links by pointing them at an internal URL, and then redirecting to the affiliate link. This should be done for a variety of reasons, and it’s an easy thing to do when you’re working with just a handful of links — but how do you make it work with a product feed where you don’t know what the link will be before you grab it?

Fortunately, masking affiliate links from a feed is easier than you think, and I’ll show you how to do it.

I only make two assumptions in this article:

  1. You already know how to parse a product feed (I can write a separate tutorial on this if there’s enough interest).
  2. Your server is running Apache and PHP is installed. Affiliate URL masking can certainly be done in other platforms/languages, but here we’ll be working with PHP and htaccess because a) it’s what I know best and b) affiliates love them some WordPress.

Getting Started

The first thing you’ll need to do is think of a dummy directory off the root of your site — this is going to be where we point our affiliate links. You can name it anything you want to, but it’s important to make sure that this directory is not actually used anywhere on your site — else you’re going to break a lot of stuff. For the sake of this example, I’m going to name my directory ‘product.’

htaccess Rules

Open up your htaccess file and add the following two lines of code (just be sure to replace ‘product’ with the name of your dummy directory, and amazon.com with the domain you will be redirecting to):

RewriteEngine on

RewriteRule ^product/(.*)$ http://www.amazon.com/$1 [L]

Save your htaccess file and close it — we’re finished here.

Manipulate the link from the feed

Now we just have to use PHP to take the product URL given in the feed and make it look like an internal link.

At this point, I’m going to assume that you’ve already parsed the feed and have stored an affiliate product URL in a variable called $url. The following line basically does a find and replace in the URL, swapping out the destination domain with your own, and adding your dummy redirect directory:

$myurl = str_replace("www.amazon.com/", "mysite.com/product/", $url);

Now when you create a link with your $myurl variable, it will automatically redirect to the product page given in your feed.

As a note, I would strongly suggest slapping a nofollow on these links, and also blocking the product directory via robots.txt.

Make It Rank runs on Genesis

genesis theme framework

Gone are the days when you had to be a developer to have precision control over your website. With the Genesis Framework, it's easier than ever to unleash your creativity and build amazing websites with WordPress. And you can do it all without sacrificing SEO control or security.

Join the StudioPress affiliate program.

Speak Your Mind

*