[Tutorial] How to Make Your Own Mass URL Shortener

Nice to see such things. I'm myself a programmer and your code is clean and easy to understand for noobs :)
 
This is awesome! How could you make money out of it?
If you did want to actually use it to make money directly then you're best bet would be go for the same model as the other shortening services do and advertise to the users of your system. Just setup a simple script like Fatboy described above and then include an optin form on the main setup page for your users. Collect their email addresses to promote your other offers to them. You could also include advertisements on the setup page, either for your own offers or charge for advertising space. I'm sure with just a little bit of thought you could think of many more ways to monetize it as well.

There's already a lot of established services out there that you'd be competing with though. I think the primary benefit for having you're own url shortener is in using it for your own campaigns rather than using something like bit.ly because it just keeps you more in control of your own promotion campaigns. It also gives you a chance to get around the limitations that some PPC and CPA companies have about using the bigger url shortening services.
 
Last edited:
Clever stuff many thanks tacopalypse.
Could this be then used as a public service like the others with ads on the site?
or would that "overheat" your hosting account?
the link looks sketchy as all hell xD

but imo, bitly links and any url shortener links look pretty sketchy to begin with.
the intention that you're hiding the destination url is pretty obvious either way.

best way to make your links 'friendly looking' is to buy a dedicated domain for it and bounce redirects off of that, but that can get expensive
I think the best use of url shorteners isn't just to hide the destination url, but to really cut down on the length of the url for posting on platforms like twitter where you're limited to the number of characters you can have in a single post. I think for that use people willing to click the link understand the need for a short url and are less likely to be afraid of it seeming sketchy. Finding a cool looking short url would still be best if possible though. :)
 
Sorry I haven't got round to putting something together to show you how to do this without MySQL, but the outline is:

1. Put a form on a page with a submit button, this is where you put your long URL.
2. The submit action then creates a random word (letters / numbers) and then creates a file with that name
3. Within the file, store the original URL
4. When someone visits your site via short URL, check to see if there is a file name of the key they are looking at (eg: http://shorturl.com/dg37su, the key dg37su). If there is, get PHP to read the file and redirect to the URL.

Thats about it - quick and dirty url shortener without using a database.....also cuts down on the hacking side of things ;) It makes it easy to start a new site with the same code.

If I get time I will try to get the scripts together over the weekend.
 
Ah okay, didn't take as long as I thought.
The files I have created are the very basics - you will need a bit of error checking on them but the scripts work! You can probably tidy the code up to be easier (probably a config file with the common things in)

Grab them from
Code:
http://fatboy.me.uk/bhw/short.tar


* EDIT *: Forgot you have to be using PHP5 and above for the code to work.
 
Last edited:
Very helpful! Installed in a subfolder under links, saves me a lot of time and gets me the traffic too.
 
Simple PHP array but great little script.
Could you track the number of visits each specific one is getting?
Like in real time.
 
Ah okay, didn't take as long as I thought.
The files I have created are the very basics - you will need a bit of error checking on them but the scripts work! You can probably tidy the code up to be easier (probably a config file with the common things in)

Grab them from
Code:
hxxp://fatboy.me.uk/bhw/short.tar

Made a quick edit to be able to bulkupload and get a list with the URLs. I am in no way a coder, just a brainiac.

#1 Create a new phpfile in the root. I called mine bulk.php. Edit YOUROWNDOMAIN to, well, your own url where the script is located.
Code:
<?php 
$list = file_get_contents("list.txt"); 
$urls= explode ("\n",$list) ; 
foreach ($urls as $array) {
$d = fopen ("hxxp://YOUROWNDOMAIN/create.php?url=".$array,"r");
fclose ($d);
}
?>

#2 Upload your list of websites you want to have shortened. Put them in list.txt with the script. Use regular hxxp://url formatting. One per line!

#3 Now run the script bulk.php by just visiting the adress where you uploaded it.

#4 Bonus - extract the shortened URL list! Upload the following and put it into list.php or whatever. Change "folder/" to whatever location you're storing the shortened linkfiles in. Also change YOURDOMAINURL to the actual URL used for the shortened links. What I mean is if you've installed the site in the root of the domain - then just use the domain. If you have any subfolders going on, just point it to domain/folder
Code:
<?PHP 
$folder = "folder/"; 
$handle = opendir($folder); 

# Making an array containing the files in the current directory: 
while ($file = readdir($handle)) 
{ 
    $files[] = $file; 
} 
closedir($handle); 

#echo the files 
foreach ($files as $file) { 
    echo "hxxp://YOURDOMAINURL/".$file."<br />"; 
} 
?>

You should now have Bulk.php (importer) / List.php (show the shortened urls) and List.txt (your long urls you want converted). Hopefully it will help you some when generating shortened urls from sitemaps, feeds or whatever you have in mind.
 
This is awesome! is there any way to make some $$ from this?
 
Vatos:

Well, always a chance to make money. This is an often debated thing, but using shorteners is a way of blasting your money site without the fear of getting "sandboxed" (actually google danced into oblivion). That's the main benefit of shorteners on another domain, which makes you $$$.

Next step to make money is of course whitehat. Think bit.ly with ads on a landing page before doing the redirection? I would probably prefer yourls to do this, though.
 
You can do it easier with .htaccess file :
Code:
Redirect /je5Hk http://www.domain.com/my-too-long-url-1.html
Redirect /k0etG http://www.domain.com/my-too-long-url-2.html
Redirect /bIu4g http://www.domain.com/my-too-long-url-3.html
Redirect /vBn2b http://wwwydomain.com/my-too-long-url-4.html
Redirect /y538r http://www.domain.com/my-too-long-url-5.html

The result will be like :
Code:
http://www.domain.com/je5Hk
http://www.domain.com/k0etG
http://www.domain.com/bIu4g
http://www.domain.com/vBn2b
http://www.domain.com/y538r

Beny
 
This doesnt seem to work. I think you left this part of the script off.

$d = fopen ("hxxp://YOUROWNDOMAIN/create.php?url=".$array,"r");


Made a quick edit to be able to bulkupload and get a list with the URLs. I am in no way a coder, just a brainiac.

#1 Create a new phpfile in the root. I called mine bulk.php. Edit YOUROWNDOMAIN to, well, your own url where the script is located.
Code:
<?php 
$list = file_get_contents("list.txt"); 
$urls= explode ("\n",$list) ; 
foreach ($urls as $array) {
$d = fopen ("hxxp://YOUROWNDOMAIN/create.php?url=".$array,"r");
fclose ($d);
}
?>

#2 Upload your list of websites you want to have shortened. Put them in list.txt with the script. Use regular hxxp://url formatting. One per line!

#3 Now run the script bulk.php by just visiting the adress where you uploaded it.

#4 Bonus - extract the shortened URL list! Upload the following and put it into list.php or whatever. Change "folder/" to whatever location you're storing the shortened linkfiles in. Also change YOURDOMAINURL to the actual URL used for the shortened links. What I mean is if you've installed the site in the root of the domain - then just use the domain. If you have any subfolders going on, just point it to domain/folder
Code:
<?PHP 
$folder = "folder/"; 
$handle = opendir($folder); 

# Making an array containing the files in the current directory: 
while ($file = readdir($handle)) 
{ 
    $files[] = $file; 
} 
closedir($handle); 

#echo the files 
foreach ($files as $file) { 
    echo "hxxp://YOURDOMAINURL/".$file."<br />"; 
} 
?>

You should now have Bulk.php (importer) / List.php (show the shortened urls) and List.txt (your long urls you want converted). Hopefully it will help you some when generating shortened urls from sitemaps, feeds or whatever you have in mind.
 
Back
Top