The Shorten library provides a quick and easy way to implement self-hosted 'short URLs' (and add the appropriate rev="canonical" / rel="shortlink" elements into your pages) on your CodeIgniter-powered site. You can implement it with just a few lines of code and use hand-crafted short versions of your URLs, let the class auto-shorten them, or use a mix of both approaches.
This document outlines the basic usage of the Shorten library - please also see the comments in the libraries/shorten.php file for further information.
This library was created and is maintained by Mark Perkins. Visit http://projects.allmarkedup.com/codeigniter_shorten_library/ for up-to-date releases, documentation and more.
This version: 1.0
Licence: Released under a MIT-style licence.
For more information and ongoing debate with regards to the URL shortening issue, please see the following sources (amongst many others!):
Please send all bug reports or feature requests to mark [at] allmarkedup.com .
To install the library, copy the libraries/shorten.php and the libraries/Exceptions.php files into your /system/application/libraries folder, and place the config/shorten.php file into your /system/application/config folder.
To create custom short URLs for your pages, open up the config/shorten.php file in your application, and under where it says $config['short_urls'] = array(); add in as many lines of short URLs as you like. The format it should take is as follows:
$config['short_urls']['shorturl'] = 'uri/string/for/full/url';
Replace 'shorturl' in the expression above with the short url you require (without leading and following slashes) and 'uri/string/for/full/url' with the full url (without the domain part and without leading/following slashes) of the page that you want the short url to point to.
That page will now be magically accessible by using the short url string (after your domain name, of course!) - ie. : http://mydomain.com/shorturl will now redirect to http://mydomain.com/uri/string/for/full/url using a 301 redirect.
So that people can 'discover' your short URLs (allowing them to use them for linking to your page), you will need to add the appropriate element into the head of your HTML page. At the time of writing, the jury is out whether this should be a <link> element with the attribute/value pair rev="canonical" or with the attribute/value pair rel="shortlink". Because of this, the shorten library provides two helper methods, printRevCanonical() and printRelShortlink() that can be used in the <head> area of your page to print out whichever you feel is most appropriate. They will lookup the short version of the url (passed in as a parameter) and print out the desired <link> element. To implement this, just put the following code somewhere in the <head> section of your appropriate view template:
<?php
$CI = & get_instance();
$CI->load->library('shorten');
// EITHER print the rel="shortlink" link element (i.e. <link src="http://mydomain.com/shortlink" rel="shortlink"> )
$CI->shorten->printRelShortlink( $this->uri->uri_string() );
// OR print the rev="canonical" link element (i.e. <link src="http://mydomain.com/shortlink" rev="canonical"> )
$CI->shorten->printRevCanonical( $this->uri->uri_string() );
?>
If you just want to get the short URL of the page returned as a string, not printed out within a <link> element, you can use the getShortUriString( $my_uri_string ) method. Please see the comments in the libraries/shorten.php file for more info.
If the Shorten config setting 'auto_shorten' is set to TRUE (it's FALSE by default), then Shorten will automatically create short urls for any pages that use the printRelShortlink(), printRevCanonical() or getShortUriString() functions in the <head> of the document. You can set the number of characters to use for the short URL in the config file. The short URL will be generated the first time that the page is visited.
The system will ensure that the randomly generated short URL will not overwrite any custom defined ones, so it can be safely used alongside hand-crafted short URLs.
The automatically created short URLs simply generate a alphanumeric string of the desired length. If you wish to set up something a bit more intelligent, you can always subclass the Shorten library, and overwrite the shortenUriString() method to create your own custom shortening formula.
The standard installation of the Shorten library described above uses a customised version of the CodeIgniter core Exceptions library, which calls the Shorten library's attemptCanonicalRedirect() function when a page is not found, but before displaying the 404 error page. If the URI string passed in is a shortened URL the library will then perform a 301 redirect to the appropriate page, otherwise the 404 page is displayed as usual.
If you don't like the way this works, then you can exclude the customised libraries/Exceptions.php file and just call the attemptCanonicalRedirect() function at some point in your controller. You would probably want to do this in the controller's constructor function, before any logic has taken place so that the user is immediately redirected. See the comments in the libraries/shorten.php file for further information on using this method.