With the outburst of Twitter and microblogging services, many companies who want to make some quick money started services that include, URL shortening like tinyurl, bit.ly, tr.im and others or image sharing services like TwitPic, yFrog and so on. However, due to lack of a proper business model, some of these sites are struggling to meet their bandwidth costs. Because most of these services are just a week long PHP programming work, it’s easy for other companies to enter the URL shortening bandwagon. As such it’s difficult for companies to really make a business model out of this.
Recently, Twitter’s switch from tinyurl to bit.ly as it’s default shortening service infuriated companies like tr.im as they announced that they will probably be shutting down their service, though they reverted their decision later. When such companies go out of business, what will happen to your URLs? Dead and gone for good? So why not host our own URL shortening services? How difficult is it to write our own URL shortening service? Turns out that a few couple of PHP files, and a MySQL database and a mod-rewrite enabled Apache server is all what you need for hosting your own URL shortening service. Writing those PHP code is not for the those who are not conversant with PHP (including me). For people like us, two smart guys, Lester Chan and Ozh Richard at yourls.org have written a custom URL shortener that you can use it for free on your blog. In this post, I will briefly explain how to host your own custom URL shortener using YOURLS. Later we will also look at how to host your own “TwitPic” service.
Steps for hosting a custom URL Shortener
Hosting your own custom URL shortener with YOURLS is a 3hr work. Yeah, believe me, these people have made most part of the work required. In fact, it took me more time to write this post than host YOURLS. I’ve explained it in four easy steps below.
Step 1
Purchase a short and sweet domain. I recently purchased a domain mk.sg and it costed me a paltry 30$ (45 SGD) from Vooju. You can explore possible domains using domainr, a nice tool that automatically checks for available domains from the letters you choose. Though this step is not mandatory, it’s good to have a short domain name given that domain names are this cheap.
Step 2
Download the latest copy of yourls from their website. Create a MySql database in your server and fill in the details of your MySQL database in the config file as explained in the config section Upload your files to the root of the server.
For example, I host my primary domain mugunthkumar.com and mk.sg in the same hosting account. mk.sg actually points to a folder inside my public_html folder. FTP these files into that folder and point your browser to http://YOUR-SHORT-DOMAIN.COM/admin. Click install. Done!
The configuration file provides options like whether it’s a private URL shortener or public, the usernames and passwords allowed for accessing the service and a whole lot. Believe me, it’s as feature-rich as bit.ly. You can track who clicked your links, from which location they clicked and a variety of other tracking features. However, it doesn’t allow users to “preview” the short links. Not a deal-breaker though.
If your installation is going to be private like mine, you can access the service from http://YOUR-SHORT-DOMAIN.COM/yourls-api.php You can rename yourls-api.php to something else as well. If you specify the installation as private in the config file, you have to provide a user name and password for accessing this API like below.
http://YOUR-SHORT-DOMAIN.COM/yourls-api.php?action=shorturl&username=test&password=test&format=simple&&url=%@
If you want your URL shortener to be used by everyone, set it appropriately in the config file and you can ignore the username and password parameter from this API.
Step 3
Install the YOURLS word press plugin that automatically links your posts, creates a short URL and tweets it. Any post or page you publish is now automatically tweeted by this plugin.
Step 4
Now, how good is a URL shortener if your favorite twitter client doesn’t support it? Fortunately with the release of Twitter 2, my favourite twitter client, you can use a custom URL shortener as described in developer.atebits.com. Open Tweetie 2, Go to Settings, URL Shorterning -> Custom, type the entry below.
http://YOUR-SHORT-DOMAIN.COM/yourls-api.php?action=shorturl&username=test&password=test&format=simple&&url=%@
Hopefully, other twitter clients will follow the crowd shortly. And I believe, if most twitter clients support custom URL shortening, the market share for bit.ly and others might go down.
With this, you can get your own custom URL shortening service up and running within, say, 3 hrs.
Next, we will look at how to host our own “TwitPic” service.
Steps for hosting a custom “TwitPic”
Hosting your own “TwitPic” is even easier. Thanks to the WordPress plugin Twitter2Press, that completely automates everything! Below are the detailed steps for hosting your own “TwitPic”.
Step 1
Create a subdomain or directory and install wordpress into it. Note that yourls and wordpress cannot co-exist in the same directory as both handle URLs differently. So install it into a subdomain and make that subdomain as short as possible. Mine is b.mk.sg.
Step 2
Install the Twitter2Press plugin and open the settings page. Choose YOURLS and specify the API username and password. This is the same username password that you provided in the config file while setting up YOURLS. Choose a page for the image gallery. This is the page that shows the images you tweeted (much like twitpic or yFrog’s home page). It can either be your home page or a dedicated page, say Image Gallery or something.
Step 3
Twitter2Press has an API that is supported by Tweetie 2 and hopefully other twitter clients will follow. For tweetie to work with your Image Service, you need to provide the API endpoint. If your Image Gallery Page is “gallery”, the endpoint is
http://YOUR-SHORT-DOMAIN.COM/gallery/index.php
All parameters are posted to this service by Tweetie 2. If you don’t have a dedicated Image Gallery Page, omit the gallery from the API endpoint.
That’s it. Now even if tr.im shuts down or twitpic eats all your files, you are safe. Your data lies in your server. Go, start yours now!
–
Mugunth
If you enjoyed this post, make sure you subscribe to my RSS feed!No related posts.
2 Responses to “Custom URL Shortening and Self Hosted “TwitPic””
Trackbacks/Pingbacks
- Abbreviare i link con YOURLS | Dubito.it Blog - [...] strumento offre la possibilità di gestire URL pubblici e privati, di impostare la generazione di chiavi [...]

Your blog contains good technical info. But it's not safe open your blog at work place as there are adult/explicit advertisements shown on the right side panel.
Thanks for this – great info