Hide Affiliate Links The PHP Way
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.





Hey Tyler, thanks for responding with a post!
What about, if I only use this code here:
php start
header(“Location: http://URL.OF.AFFILIATE.LINK.HERE“);
exit();
break;
php end
Will that work as well?
thanks
If you point the link to a page with just that yes.
So
< ?php
header("Location: http://URL.OF.AFFILIATE.LINK.HERE“);
exit();
?>
Would be all you needed. Call it redirect.php and then just point your affiliate link to that. When people click on the link the header() function will take them straight to the page they affiliate link without being able to change the URL before they get there.
Will the above work with affilite links that are written in javascript like the ones from TLA?
Which I can not get to work on my blog.
Which part of TLA uses javascript? The links I had to refer people to them using my ID are all URL based.
Do they have a widget that you can click on that’s got javascript in it?
The thing to be careful about here is to make sure that you verify that they’re coming from your site, otherwise your redirect script could be used for less than ideal purposes by those with shady morals.