Last week at work some marketing guys asked me if I could build a URL Shortener script.
They did not want to use bitly anymore.
I told them that I had a url shortener sites ones (snurl.eu) and that I could probably could find the script again.
When I found the script and thought about MongoDB… I guess it was time to rewrite it.
So when I had the internet site Snurl.eu I wanted to have my own URL shortener service. I was kinda lazy so I searched for a PHP script that did just wat I wanted.
So I stumbled upon the Script build by Brian Cray. His “Free PHP URL shortener script that kicks ass” as he called it was perfect. Build in 2009 so a bit old but it did the trick.
Now 6 years later I rebuild it. Why….? well I just love MongoDB. Don’t get me wrong. MySQL is great… but for simple and big ass databases.. with hits-updates…. well you don’t want to use MySQL anymore! You want a NoSQL version!
So…. when my colleagues asked me for this Url Shortener script, I pulled the Brian Cray script and did a little rebuild 🙂
It’s still has these great features (and more!):
- Can shorten over 42 billion unique URLs in 6 or less characters or if you want it can do 12.000.000 in only 4 characters!!!
- It’s even super duper super fast, as it uses MongoDB and uses like almost no server resources
- Yes, it does include an API, for creating short URL’s on the fly!
- Wanna count those visites? Just turn it on!
- Option to limit to one ore more IP addresses for personal use and to prevent spamming!!
- It uses only alphanumeric characters so all browsers can interpret the URL (yes even IE)
- SQL injection hacks?? Ha ha ha it’s Mongo!!!
- Url realness checker! No more 404’s
- Uses 301 redirects for SEO.
- Wanna cache? Not needed with MongoDB, but still an option!
- Wanna use your own short url? Just send it with the Long Url!
Installation is easy!
Make sure your server meets the requirements: Run this from your own (short) domainn
Download the script from Github
- Upload the files to your web server
If you want to use the caching option, create a directory named cache with permissions 777
Using your personal URL shortener service
To manually shorten URLs open in your web browser the location where you uploaded the files. You should see a simple form to add your url, which will return a shortened version.
To programmatically shorten URLs with PHP use the following code:
$shortenedurl = file_get_contents('http://yourdomain.com/shorten.php?longurl=' . urlencode('http://' . $_SERVER['HTTP_HOST'] . '/' . $_SERVER['REQUEST_URI']));
I like to thank Brian Cray for his versions.