Creating and Installing Your Own URL Shortening Service

By Lee Underwood

URL shortening services such as tinyurl.com, bit.ly, and poprl are very popular and have a wide array of uses across the Web. For instance, shortened URLs can be used in e-mail and forum postings. It's also advisable to use shortened URLs in newsletters as many spam filters sometimes interpret several, long URLs as spam. And shortened URLs are, of course, a requirement when using Twitter. In addition to saving space, shortened URLs also make for easier reading.

In addition to the basic URL shortening services, many organizations have gotten into the act, e.g., Facebook, Google, even the Republican party. However, you might want to consider where some of these services originate. As Wired.com recently noted, some of these services are in countries that support terrorism, e.g., Bit.ly. "ly" is the country code for Libya, a country with ties to terrorism. You can find a list of country codes on the IANA Web site.

Roll Your Own

Instead of concerning yourself with all of those details, you could just create your own URL shortening service. You can offer it for use by the general public or for your own personal use. In addition, this is a great method for getting the URL of your Web site spread around the Web. Although the actual link would usually go to a different site, your main domain would still be there, e.g., example.com.

There are many different scripts to choose from. One of them is an easy-to-use script created by Harry (yep, just "Harry."). It's call "Harryjerry Linx" and is very simple to implement. Harry is currently working on additional features to enhance the usefulness of the script. All you need is a Web hosting account with PHP/MySQL support.

Installing the Script

First, you will need to download the script and uncompress the file.

Next, upload all the files to your server. It's best to use one letter for the directory name where the files reside to keep it short as it will be included in the shortened URL, e.g., http:yoursite.com/l/r2. Be sure to place the includes subdirectory (included with the compressed file) inside of this directory. Here is a list of the files, with their location, that you will need to upload (if you are using a directory titled "l"):

  • /l/.htaccess
  • /l/database.sql
  • /l/index.php
  • /l/includes/conf.php
  • /l/includes/hjurl.php

Next, create a MySQL database on your Web server. If you've never done this before, and have a Web host that is using cPanel, it's really pretty simple. (There is also a great MySQL video tutorial available for all the steps below.)

To create the database, go to the database area in cPanel and select the "MySQL Database Wizard." Enter a name for the database, e.g., shorturls. (The name can only consist of letters and numerals.) Click the "Next Step" button. Now, before adding a user, look at the top of the page. Here you will be able to see the actual name of the database. Make a note of this as many Web hosts add the name of your account to the beginning of the database name and you will need to know it later. For example, if the name of your account is "yoursite" and the name of your database is shorturls, then you will probably have something like yoursite_shorturls shown as the actual database name. (On shared servers, this allows the system to easily sort out the databases, displaying only the ones for your account.) Now, you will need to create a username and a password. Usernames cannot be more than seven characters and can only consist of letters and/or numerals. After pressing the "Next Step" button, be sure to make note of the actual user name on the next page. Once again the Web host may have added the name of your account to the beginning of your user name. To select the privileges you want for this account. just select "all." Before pressing the "Next Step" button, you may want to print out the page, as it will probably have all your pertinent information, e.g., database name, username and password. Keep this information safe for future reference.

Once you have created the database, open the conf.php file found in the /includes directory on the server. You will need to add your database information here. Look for this section:

define('MYSQL_USER', 'harryjrc_linx');
define('MYSQL_PASS', 'pb650917');
define('MYSQL_DB', 'harryjrc_linx');

On the first line ('MYSQL_USER'), replace harryjrc_linx with your database username. On the second line ('MYSQL_PASS'), replace pb650917 with your database password. Finally, on the last line ('MYSQL_DB'), replace harryjrc_linx with the name of your database. Be sure to include the full name of each of these, as given by the Web host, if applicable.

Finally, you'll need to import the contents of the database.sql file into the database. You should use phpMyAdmin if it's available. If you're using cPanel, go to the database section and click on phpMyAdmin. Select your database from the menu on the left hand side. Then, select the "Import" tab. Under "File to import," browse to and select the file database.sql. Then press "Go" in the lower right-hand corner.

Testing the Script

Now, if you point your browser to http://yoursite.com/l/ it should bring up the page with the URL link shortening generator on it. It should look like this page. After creating a shortened URL, you will need to reload the page to create a new one. A quicker method is to add the following code between the line where the form ends, e.g., </form>, and the line with "Powered by" in it.

<p><a href="http://yoursite.com/l/">Another one</a></p>

It's nothing magical; it'll just reload the page. But it's easier to click on the link than to go to the reload button in the browser (do not remove the "Powered by" line unless you want to donate money for the script). You can change the page to look anyway you want to.


That's it! There is nothing left to do but to start shrinking those URLs! If you're offering the page to the general public, you can change the page to anything you like. Just be aware that there is not a lot of security with this script, as far as stopping others from using it. But if you don't tell others, they won't really know it's there. However, you shouldn't really have any problems with it. If you have any questions, you can ask Harry. You can also leave a comment if you like the script.

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
Thanks for your registration, follow us on our social networks to keep up-to-date