• How to create your own private URL redirect/shorten service

    by  • November 7, 2011 • PHP • 0 Comments

    Originally Posted on Dec. 29, 2009

    A little while back, I began doing some research on how to create my own URL shortening service that uses a mysql database to store/retrieve the new URLs and automatically redirect the visitor to the correct URL.

    The problem I encountered was they were all very complicated and difficult to understand examples that I found.
    Well, since then I’ve managed to learn regular expressions and I’ve also discovered sqlite (A flat-file database). So I decided to see if I could make my own very simple redirect service using what I know with PHP and here’s what I’ve managed to accomplish.

    The short-url files:

    This script consists of 3 files to work:

    1. index.php – This is the primary file that contains all of the logic and code that makes it all work.
      It has the form and php code that processes the requests.
    2. .htaccess – This file is very important for the script to work correctly. It contains the rewrite conditions for the urls and MUST be present for the re-directs to work correctly.
    3. db.sqlite – This file will be created if it doesn’t exist. This is the sqlite database file where all the data is stored. It should be set to read/write permissions by the server, which usually is the group www-data.
      The table needs to be created by index.php when first setting it up. Refer to the notes in the index.php code for more info.

    Short URL Hashing

    If you want to know the magic of how they convert http://adomain.com/some/long/url/ to something like http://bit.ly/s4908Vs, the trick is creating a URL hash. You can use different hashing algorithms to get different length results, but it basically takes the URL and creates a unique hash for it, which ensures duplicates won’t be made.

    The tutorial regular expressions has some good info as well as regular expressions.

    How it works:

    The way this script works is by utilizing Apache’s mod_rewrite extension to determine if the url is a request and if so, it retrieves the url from the sqlite database and sends a 301 (permanent redirect) header to the user’s browser and takes them to the correct URL.

    In order for the code to match, the shortened URL for this script MUST not contain any characters that aren’t numbers or letters. Otherwise it will not forward the user and instead try to locate the actual file locally. Note: You could change it to better accomodate the Apache’s mod_rewrite, but that’s beyond this tutorial.

    http://mydomain.com/ade45612rt/ or http://mydomain.com/ade45612rt will work.
    http://mydomain.com/ade45612rt.html will not since it has the period in it.
    It will instead try to locate the actual file ade45612rt.html on the server.

    The rewrite code in the

    file matches the first example, then performs a GET request on the database to find the actual URL matching the value ade45612rt and sending that in the redirect header.

    You may also specify a custom url if you do not wish for the script to auto-generate a 10-character string automatically.
    The custom url may contain “_” or ” -” for spacing. If you enter empty spaces in it, it will be replaced with underscores “_” automatically.

    Here’s the code:

    .htaccess file

    index.php file

    Known Issues & Requirements

    This script is NOT written for production/public use. It is written for basic function only.
    So what does this mean? DON’T USE IT ON A PUBLIC SERVER AS-IS!!!!

    • The script is not password-protected, which means anyone can access the form and create a URL from your site. This can pose as a problem since spam could also be used.
      It also allows anyone to see all of the posts in your database.
    • This will ONLY work on Apache servers with mod_rewrite enabled. Some Apache servers may not have this enabled and this won’t work at all on IIS servers.
    • sqlite module must be installed for PHP. The code could easily be changed to mysql if sqlite isn’t available on your server.
    • There’s no filter for incorrect custom urls, meaning something that detects if they’ve entered “.html” or “.php” in the custom url field. Since it won’t work if any special characters are added, it should be filtered and then alert the user if it’s incorrect.
      Otherwise the rewrite condition in the .htaccess should be modified to accommodate special characters.
    • There’s nothing checking that the randomly generated string is a duplicate or not, meaning that duplicate entries are possible.
      I suggest adding date/time to the function to ensure that it is a unique value or else check the generated string against any current entries.

    Bottom line is this is a very simple script that should work fine if all the conditions are met.

    Further Reading/Related Info

    If you want to create your own public URL shortener, there’s some really great turn-key type scripts and tutorials out there. Here’s a few I’ve found and even use one or two myself:


    I'm a freelance PHP developer spending much of my time deep in WordPress theme and plugin development. My personal info can be found at http://amereservant.com .


    Leave a Reply