Link Cloaking Code

Status
Not open for further replies.

Icecube

Up 24h/day
Mar 14, 2007
1,169
9
0
Europe
hey bitches, it's time I give something back to WF, here's the code I use to cloak affiliate links
basically it works this way:
there's a database table where you put all your affiliate links and the codes you want to use in place of the real link
there's a small php script that runs the query and pushes the visitor to the affiliate site
I use an .htaccess file to mask everything so I can use links like mydomain.com/bluebananas and the visitor will be pushed to my affiliate bluebananas merchant

I don't know if this works on windows servers but it should as long as it's running apache
in case your server runs on IIS I don'tknow if you'll be able to use the cool sexy links but the script will work anyway: you'll just need to make your links to go.php?d=bluebananas

first thing open phpmyadmin, create a database and past this into the SQL box
Code:
CREATE TABLE `links` (
  `id` int(4) NOT NULL auto_increment,
  `code` varchar(20) NOT NULL default '',
  `url` varchar(200) NOT NULL default '',
  `visits` int(11) NOT NULL default '0',
  PRIMARY KEY  (`code`),
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM
this creates the table

let's see the .htaccess file
Code:
<Files .htaccess>
    Order allow,deny
    Deny from all
</Files>
Options -Indexes +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([-a-z0-9_]+)/?$ go.php?d=$1 [NC,L]
it takes anything after the domain and rewrites it as a variable for the php script
example: mydomain.com/bluebananas becomes mydomain.com/go.php?d=bluebananas

now, this is the php file that will actually push the visitor to the affiliate link
Code:
<?php
$connect = mysql_connect(localhost,dbusername,dbpassword) or die("Error connecting to Database! " . mysql_error());
mysql_select_db(dbname , $connect) or die("Cannot select database! " . mysql_error());
     if (isset($_GET['d']))
     {
       $dest = $_GET['d'];
       $query = mysql_query("SELECT url FROM links where code='$dest' LIMIT 1") or die(mysql_error());
       $count = mysql_query("UPDATE links SET visits = visits + 1 WHERE code='$dest'") or die(mysql_error());
       while ($row = mysql_fetch_object($query))
       header("Refresh: 0; url=".$row->url);
     }
     else
     {
       header('Refresh: 0; url=http://www.mydomain.com');
     }
?>
it takes the parameter and runs a query retrieving the real affiliate link, then refreshes the visitor's browser to that link
if, for any reason, the query doesn't return any link the visitor is sent back to the home page

to add my affiliate links I go directly through phpmyadmin, it's just 2 fields, not worth spending time writing a page to do that (although I have something that might almost fit sitting in some corner of my hard disk)
 


Very cool! I like how you added the stuff for visits!

I have a question about the .htaccess though. What does the "!-d" and "!-f" do? Is the "!-f" even needed?

Is the last line in the .htaccess needed or is that just an additional feature you included?
 
yeah I forgot to mention that the script counts the visits you've sent to each link

!-d and !-f are checks that tell apache not to apply the rule in case there's a directory(d) or file(f) with that name
if you use the link mydomain.com/bluebananas but you actually have a bluebananas directory you'll be shown the directory content and you won't be pushed to the aff link
the last line of the .htaccess is necessary, it's the rule that makes everything work
 
I suck at programming and databases.
I assume this will work for as many links as you want?
Is it easy to add more links later on? Just add them to the DB?
 
So the bottom line in the .htaccess file is only for the feature that allows you to redirect www.example.com/redirect in the same fashion as the go.php?d=redirect link. I really don't want to redirect stuff like that, I only want it to work with the go.php?d=redirect fashion, so I don't really need any of the stuff from that htaccess file do I?

Also, I decided to use

header("Location:$row->url");

Instead of the header refresh method. I don't think there is really a difference though is there?
 
@natekapi:
yes, you don't need the htaccess file then
I don't know if there's any difference between the 2 redirects

@comatose:
you can add as many links as you want, just go to phpmyadmin, select this table, press insert at the top and fill the form with the data of a new link
 
It's funny because a lot of people think of cloaking as being some really advanced thing. Nope. :D

It's just a redirect, and there are thousands of ways to do that. This is a great way if you have a bunch of affiliate links.
 
does this work for 1and1 host? I have everything setup but it does not seem to be working properly

I have never used 1and1, but if your plan comes with php and mysql it should work (assuming 1and1 doesn't set any general restriction on .htaccess)

it should work on ANY linux server with apache, php and mysql
should work as well on any windows host with the same features but I'm not sure about the htaccess on a windows server
 
You may need to upgrade your 1and1 hosting account to get access to MySQL Databse and php.

thanks its on a lunux server as I do not trust windows hosting. but everything looked like it looks good but when testing, I got my 404 error

http://www.ianfernando.com/suggests/WICKEDFIRE

this should turn the URL to .....go.php?d=WICKEDFIRE

checkin the php which checks the sql databse and pushes the url?

that is what I am understanding, I will email 1and1 and see why it is not working
 
I tried accessing http://www.ianfernando.com/suggests/go.php?d=WICKEDFIRE and you have an error on line 2
you're getting the 404 error because you actually created a suggests directory (remember that I don't have a physical directory named "go") remove it and place go.php in your main directory (assuming you put the htaccess code in the main htaccess of your site)

http://www.ianfernando.com/suggest/ultimatewealth
http://www.ianfernando.com/suggest/go.php?d=ultimatewealth
that is one link I am still trying to test and see why its not direceting properly
the directory created was suggest without the 's'
seems not to be working properly - I am looking at the SQL tables and it looks perfect
so I am not sure what is going on unless it has something to do with the htaccess or php


I kinda figured that in the beginning so I created a new directory with the htaccess in that specific directory and I created go.php in that directory as well

so when I created the htaccess in the suggest directory I would assume that it would think that is the main directory

katoved: I have a business plan with 1and1 and I have access to my sql and it supports php as well
 
Last edited:
so, in this specific case, try to use this rule
Code:
RewriteRule ^/?([-a-z0-9_]+)/?$ /suggests/go.php?d=$1 [NC,L]
and see if it works, and fix the error on line 2, most probably unclosed parentheses or wrong punctuation in go.php
 
so, in this specific case, try to use this rule
Code:
RewriteRule ^/?([-a-z0-9_]+)/?$ /suggests/go.php?d=$1 [NC,L]
and see if it works, and fix the error on line 2, most probably unclosed parentheses or wrong punctuation in go.php


cool thanks - damn almost - its saying it can not see my server

Error connecting to Database! Unknown MySQL Server Host 'xxxxxxx' (1)

now host - has '.' xxx.xxx.xxx

that is how my host is setup and it removed the . from my host and each SQL has to log on to a dif host - so localhost does not work

I did not try to re arrange the htaccess yet - since now this is a new issue that just came up now

thanks
 
Great thread! I was just about to look for a free redirection script, using this will help me learn more php/sql instead of just installing a script!

Just to check - if I wanted to add a comments field it would just be something like this?

Code:
CREATE TABLE `links` (
  `id` int(4) NOT NULL auto_increment,
  `code` varchar(20) NOT NULL default '',
  `url` varchar(200) NOT NULL default '',
[B]  `comments` varchar(200) NOT NULL default '',[/B]
  `visits` int(11) NOT NULL default '0',
  PRIMARY KEY  (`code`),
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM
 
Status
Not open for further replies.