Redirection script with PHP and HTACCESS to redirect URL (affiliate links)

1 post / 0 new
Redirection script with PHP and HTACCESS to redirect URL (affiliate links)

This is not really about content writing, I should have put it in the Clickbank forum, but it is very general and applies for everything, mainly affiliate links, when you want to hide the affiliate link to make it appear like it is on your site.

I won't go too much into why you'd want to hide your affiliate link, and also why you wouldn't want to use url shortener third services, or any third party service indeed..

No, if you're looking for a redirect script, you already know why you want that..

Well I wanted it, so I've worked a little on it, I wanted to do something that can optionally accept TAG ID for tracking purposes..

So, first I give you the structure and workings I wanted to have..
I wanted to have all links on my site, so it would look like:

folder1 would be a name like "recommends" or "articles" or "go".. Recommends is often seen on affiliate marketers sites, I think it is because of a third party script, or because affiliate marketers copy whatever script example they've seen, I don't know really the reason.

folder2 would be a name such as "truth-about-abs" if you're promoting the Truth About Abs clickbank product..

and tag would be any tag you want to use as a tracking id, so if you make a sale you can have an idea where the lead comes from (a specific article of yours, or a special ad location, an email, etc.)

So, my folder structure is simple, I have folder1 and inside I have many folder2, one for each product. And in each folder2 I have two files:
- .htaccess (it is simply to take the url, see if there is a "tag" and if so, pass it to my second file, a php script) this file is exactly the same in all folder2 folders.
- index.php (this file is basically the same in all folder2 folders, except for one line at the top of the script and maybe some changes in the script depending if the link is a clickbank link or not)

First I'll give you the content of the .htaccess file, it is easy:

RewriteEngine on
Options +FollowSymlinks

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?tag=$1 [L,QSA]

you see the last line simply says to take everything that is in the url after the "folder2/" and send that part to the index.php script as the value of the argument "tag"
So for instance:
if I go to it will call
if I go to it will camm

Now, the content of the index.php script:

// set $myurl to where the user should be redirected
$myurl = '';
// mydebug: 1 to see values and no redirect, 0 for normal behavior
$mydebug = 0;

// get tag (for tracking purposes when needed)
if ( isset($_GET['tag']) && ($_GET['tag'] != '') ) {
	$mytag = $_GET['tag'];
	// for clickbank
	$mytag = '/?tid=' . $mytag;

$myredirect = $myurl . $mytag;

if ($mydebug == 0) {
	header("Location: $myredirect");
} else {
	echo "mytag: $mytag <br />myurl: $myurl <br />myredirect: $myredirect";

So, I'm going to explain what it does, but if you can read basic PHP you can understand it since I can only write basic PHP :-)

First, I've put the first line to set the variable $myurl, this is the url of the clickbank affiliate url, so that's the only line that is changing in all index.php script in each folder2 when those are for clickbank..
If one url is not for clickbank, then you should not use the tag feature, or you should workout what to change after the linke // for clickbank and adapt it to your particular tracking id, for the affiliate service/network you're using..
If you understand that's fine, if not you should send me a comment below and I'll edit this page or reply to you in the comments. I think it is not hard to understand that's why I don't spend time explaining something like that, but if you need it, don't hesitate to ask me if you don't require an answer immediatly.

So what this script does? Simply redirect the visitor to the product affiliate url that you put in the first line.
In case there is a tag, tracking id, then it appends it to the url before redirecting it.

Also, the script is a little more complex than it could be, because I've used a variable at the top called $mydebug it is simply if you want to see where the url should be redirect, so you set mydebug to 1 and it will show you the different variables instead of redirecting. If you let $mydebug to 0 then it will work as normal and redirect like it should.

Well, I've just finished this script, so now I'm busy selecting the different products I want to promote from the clickbank marketplace and setting my folders, this will take me some hours of work, then I have to do the same for other affiliate networks and maybe update my script somewhat..
Also I am doing another script which will display ads, I have chosen two main formats, 300x250 and 728x90, so in each folder2 I'll upload graphics in those 2 sizes and will have one script to display them along with the link, but that's another story, nothing to do with this simple but efficient redirect setup.

I hope it will be useful to some of you, if you were looking for such a working example.. But if you have any suggestion or critic or comment, please do take the time to send me a comment below, I'm not an expert about anything so I welcome any level of help there, thank you!

Your feedback is important to us! We invite all our readers to share with us their views and comments about this page.

The content of this field is kept private and will not be shown publicly.
Please answer the question, even if it looks like a very stupid question, it filters out most of the spam.