How To Do An Outbound Link Script For Affiliate Links To Sponsors
In the last post I discussed how to do a script to randomly display banner ads on a site. The next step is how to handle the clicks on those ads (or on text ads).
You might just think you should embed the links directly in the HTML, but that's often a bad idea. Let's say your sponsor runs a special promotion that uses a special promo link code. You may have the links all throughout your site in blog posts, banner ads, sidebar text ads, etc. There will be too many things to change to take advantage of the special promotion. Having all your outbound sponsor links go through a single script will fix that problem - change things in one place and all your links are changed.
The way this works is we want to pass into a script URL the ad number (or name) that you want to display. So your URL would look something like this:
http://www.domain.com/scripts/ad-links.php?adid=1234
So ads.php is the script we'll create, and adid is the ad ID for your outbound link.
Sometimes you have to enter other parameters into the ad URLs to meet sponsor guidelines, You can do that as well with a URL that looks something like this:
http://www.domain.com/scripts/ad-links.php?adid=1234¶m1=ABC¶m2=XYZ
So in that case the sponsor is asking for other information that may vary depending on the context of the ad, so there are two parameters that you can pass into the script which you can insert into the code displaying the banner ad - param1 and param2.
Now, before you start it's a good idea to think about what you want to do with search engine spiders. Search engine spiders don't want to see paid links and affiliate links to sponsors definitely fall into the "paid links" category. You have two options. One is to exclude them completely using robots.txt, the other is to let them crawl the links, but send them to a different URL than a real customer would go to - send them to a relevant page on one of your own sites.
So, let's say your link is to a company that sells pop corn. The best page to send the search engine spider to would be the tag page on your blog that has all the posts about the different types of pop corn offered by the company in question. If you don't have a page like that then you could send them to the page with all the posts about their company or a page with all the different pop corn reviews you might have. The point is, keep it relevant. If you keep it relevant the search engines shouldn't say you're cloaking. If you don't have a relevant page, then 302 them to the main page of your site. The 302 redirect doesn't pass page rank, so this should be a harmless way of keeping them away from paid links without them thinking your playing page rank sculpting games to deceive them.
Now, onto the script... Here's a brief version of the script:
<?php
if (stripos($_SERVER['HTTP_USER_AGENT'], 'googlebot') > 0) { $spider = TRUE; }
elseif (stripos($_SERVER['HTTP_USER_AGENT'], 'msnbot') > 0) { $spider = TRUE; }
elseif (stripos($_SERVER['HTTP_USER_AGENT'], 'teoma') > 0) { $spider = TRUE; }
elseif (stripos($_SERVER['HTTP_USER_AGENT'], 'slurp') > 0) { $spider = TRUE; }
else { $spider = FALSE; }
/* Pop Corn Sponsor */
if (($_GET['adid'] == 1) && ($spider)) { header("Location:http://www.your-domain.com/tag/pop-corn-sponsor-posts",TRUE,301); }
elseif ($_GET['adid'] == 1) { header("Location:http://join.pop-corn-sponsor.com/track/ODM4NzA1NzozMzoxNQ/",TRUE,302); }
/* Pop Corn Maker Sponsor */
elseif (($_GET['adid'] == 2) && ($spider)) { header("Location:http://www.your-domain.com/",TRUE,302); }
elseif ($_GET['adid'] == 2) { header("Location:http://join.pop-corn-machine-sponsor.com/track/ODM4NzA1NzozMzoxNg/",TRUE,302); }
else
header("HTTP/1.0 404 Not Found");
echo "<html><head><title>Link Error</title></head><body>Link Error<br />".$_GET['lnk']."<br />".$_SERVER['HTTP_USER_AGENT']."</body></html>";
?>
You can see why we started with a discussion of search engine spiders - it's the first thing the script deals with - the first if statement tests to see if the request is coming from one of the 4 big search engine spiders. If so, it sets the $spider variable to a value of TRUE.
Then there's a if, elseif statment where we go through all the possible adid values. There are two cases for each value - one for what do do with a spider, and the other for what do do with regular users. The PHP header function is used to do 301 (permanent) and 302 (temporary) redirects. Notice that I always send people off to other sites with 302 redirects (so no PageRank is given to the sponsors site), and I also use 302 redirects when I don't have a good page on one of my sites to send spiders. 301 redirects are only used for relevant redirects for spiders so PageRank doesn't accumulate in the redirect script itself.
And lastly there's an else case for what to do if I don't find the adid. First I want to indicate that the link was not found with a 404 (page not found) response code. This makes sure the error will show up as an error in my analytics. Next, I want to tell the user what happened with some very simple HTML code.
So there you have it - an outbound link script that's easy to maintain and SEO friendly.
Tags: CityTech ADV 4850, PageRank, Paid Links
Categories: Advertising, PHP, SEO/SEM