Sunday, October 17, 2010

How To Build Your Own URL Shortening Service

Wouldn't it be nice to have your own URL Shortening service like bit.ly or tiny.url?  It may be more than nice, it may be critical.  If you are using URL shortening services for tracking clicks on a link for your business or you want your links to remain shortened "forever" having your own URL shortener can be a very inexpensive solution.

One of the best OpenSource (free) scripts I have seen for doing this is Yourls.   You can download the code at google code.  The current version is 1.4.3.  It is a PHP script that requires at least PHP 4.3 and at least MYSQL 4.1.  If you want to mess around with the API (yes it does have an API) it is recommended that you have Curl extension.  It comes with a Wordpress plugin so that your Wordpress posts can be automatically Tweeted using your own URL Shortener (sweet).

Your first step is to get a really small URL.  Be creative.  Use top level domains like .me .ws .it .in .co or if you plan on using your own URL use a subdomain like s.mydomain.com (don't use www).  You might try using Domai.nr to help you find good domain names. 

As far as installation is concerned it is pretty straight forward.  The instructions are located here.  Probably the most important thing is to set up the database and the config file. 

When you set up your MySQL Database remember to write down or remember the database name  database user and database user password.  You will need these value for the config file.  When you create the database you do not need to worry about creating tables.  The script will do this for you. 

When setting up the config.php file (located in the includes file please note these important settings that you will need to set in red text.

MySQL settings

  • YOURLS_DB_USER
    your MySQL username
    Example: 'joe'
  • YOURLS_DB_PASS
    your MySQL password
    Example: 'MySeCreTPaSsW0rd'
  • YOURLS_DB_NAME: The database name
    Example: 'yourls'
  • YOURLS_DB_HOST
    The database host
    Example: 'localhost'
  • YOURLS_DB_PREFIX
    The name prefix for all the tables YOURLS will need *DON'T CHANGE THIS VALUE
    Example: 'yourls_'

Site options

  • YOURLS_SITE
    Your (short) domain URL, no trailing slash
    Example: 'http://whatever.com'
  • YOURLS_HOURS_OFFSET
    Timezone GMT offset
    Example: '-5'
  • YOURLS_PRIVATE
    Private means protected with login/pass as defined below. Set to false for public usage (on an intranet for instance)
    Example: 'true'
  • YOURLS_UNIQUE_URLS
    Allow multiple short URLs for a same long URL
    Set to true to allow only one pair of shortURL/longURL (default YOURLS behavior), or to false to allow creation of multiple short URLs pointing to the same long URL (as bit.ly does)
    Example: 'true'
  • YOURLS_COOKIEKEY
    A random secret hash used to encrypt cookies. You don't have to remember it, make it long and complicated. Hint: generate a unique one at http://yourls.org/cookie
    Example: 'qQ4KhL_pu|s@Zm7n#%:b^{A[vhm'
  • yourls_user_passwords
    A list of username(s) and password(s) allowed to access the site if private
    Example: 'joe' => 'mypassword'

URL Shortening settings

  • YOURLS_URL_CONVERT
    URL shortening method: base 36 or 62.
  • yourls_reserved_URL
  • A list of reserved keywords that won't be used as short URLs. Define here negative, unwanted or potentially misleading keywords
  • Optional settings
  • YOURLS_PRIVATE_INFOS
    If YOURLS_PRIVATE is set to true, you can still easily make your individual stat pages public. To do so, override the global private setting with:
    define('YOURLS_PRIVATE_INFOS', false);
  • YOURLS_PRIVATE_API
    If YOURLS_PRIVATE is set to true, you can still easily make your API public. To do so, override the global private setting with:
    define('YOURLS_PRIVATE_API', false);
  • YOURLS_NOSTATS
    If YOURLS_NOSTATS is set to true, redirects won't be logged and there will be not stats available.
Here are some cool examples of sites running Yourls:

  • http://hmm.ph/
    Funny domain name, nicely styled
  • http://qte.me/
    Another YOURLS setup with a cute quote look
  • http://mrte.ch/
    Mr Tech's public YOURLS setup, listing top links and some stats
  • http://s4c.in/
    Shorten 4 Charity, a public YOURLS setup with shortened links displayed within a frame containing ads for charities
  • http://girv.in/
    John Girvin's YOURLS setup with all links in a sortable display
  • http://www.iyeman.net/
    Example of portal for your online self with links to your places (blog, Twitter) and your URL shortener
  • http://li.nkto.me/
    A nice domain name hack :)
  • http://vb.ly/
    Public YOURLS setup run by famous sex writer Violet Blue as "the internet's first and only sex-positive url shortener"
  • http://chort.in/
    A neat take on the word shorten. An interesting little site.

1 comment:

  1. Did you know that you can create short urls with AdFly and earn dollars for every click on your short urls.

    ReplyDelete