marcus welz

Implementing short URLs using case-sensitive Routes with Zend Framework

Posted on July 6, 2009

Since short URLs are all the rage these days, I wanted to outfit one of my websites with its own short URL capability.

A simple way to accomplish this is to base62 encode a numeric identifier (the database table's primary key). In order to identify a URL as a short URL, it will be prefixed with an uppercase 'S'. So, a short URL would be something like this: http://mixoom.com/Snfup8.

With regular expressions it's simple to turn off and on case sensitivity using (?-i) and (?i) respectively. So ultimately the new route needed in order to accomplish mapping the short URL is the following:

$route = new Portal_Controller_Router_Route_Regex(
    '(?-i)S([\w\d]+)',
    array('controller' => 'photos',
          'action'     => 'shorturl'),
    array('shortid' => 1)
);
Print This Post Print This Post
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

No trackbacks yet.