Website Magazine

Hide Affiliate Links The PHP Way

Writen By Tyler Ingram on Oct 15, 2007

A recent comment that was left in one of my posts was asking how they could utilize PHP to create a redirect for their Affiliate links. After a few moments of brainstorming a decent solution presented itself to me. Using a couple of lines of code (of course depending on how many affiliates you use) you can redirect people to any location you wish. To take it one step further I will also show how to make use of the .htaccess file on your server (granted that you’re using Apache as web server) to mask the links and make them SEO friendly as well.

The PHP code to redirect (created in a file called redirect.php):

[code]
<?php
if(isset($_GET['aff'])) {
  switch($_GET['aff']) {
      case "tla":
    header("Location: http://URL.OF.AFFILIATE.LINK.HERE");
    exit();
    break;
     case "reviewme":
    header("Location: http://URL.OF.REVIEWME.AFFILIATE.LINK.HERE");
    exit();
    break;
  }
?> [/code]

 

The line if(isset($_GET['aff'])) checks the URL for a the argument aff as in: http://www.tyleringram.com/redirect.php?aff=tla

The line switch($_GET['aff'])) will process the argument and handle it differently based on what it finds the argument to be.

Now if you want the URL to be SEO friendly you can do so by editing your .htaccess file in the root of your web site on you server.

Editing .htaccess file to work for masking or hiding Affiliate link:

[code]
RewriteEngine On
RewriteRule ^go/([A-z0-9\-]+)$ redirect.php?aff=$1
[/code]

If you already have RewriteEngine On in your .htaccess file you will not need to add that line in again. What we are telling Apache to do is convert one URL to act as another. So when you enter http://www.tyleringram.com/go/tla, Apache will process this and call the redirect.php file and anything after the last slash will be used as the argument for the page.

If you have any comments, suggestions about the above post in regards to using PHP to create an Affiliate redirect please leave a comment.

 

Posted in: Blogging| PHP| SEO | 1,086 views

 5 Comments

What do you think? Join the discussion...






How do I change my avatar?

Go to gravatar.com and upload your preferred avatar.