Creating a custom short-URL service (post 2 of 2)

In a previous post, I talked about using bit.ly to create a hassle-free custom Short-URL service for go.cris.lc.

I have since discovered a hassle. Although bitly will use your unique URL for any links you post (e.g., go.cris.lc/WorkProfile instead of bit.ly/WorkProfile), it maintains one master database for all users of shortlink names (e.g., “WorkProfile” has to be unique across all bitly users).

One of the reasons I originally obtained cris.lc was to build unlimited, unfettered convenient shortlinks, like “cris.lc/blog” or “cris.lc/WorkProfile“. Being bound by other Bitly users’ pre-existing shortlinks was unacceptable.

As a result, I went and installed YOURLS (Your Own URL Shortener) on one of my domains. This could create some complications if/when I try to build my own URL shortener from scratch, but I’ll cross that bridge when I get to it.

YOURLS is extremely easy to set up (although I did have to file a bug about the installation instructions on their website). It also has the option for generating MiXeD CaSe or all lowercase short URLs. What it LACKS, however, is the option for case insensitivity. For instance, there is no built-in option in YOURLS if I want http://cris.lc/WorkProfile to be the same as http://cris.lc/workprofile (or, for that matter, http://cris.lc/WOrkpROfILE).

For the best fit, it seemed sensible to set YOURLS’ config to generate only lower-case shortlinks AND to automatically force-lowercase all URLs that are sent to it for redirection. That way I could be certain that any MiXeD CaSe URLs sent to cris.lc would be purely cosmetic. Unfortunately, after spending some time investigating rewrite rules in Apache, I discovered that the function I needed (RewriteMap) can’t be loaded in .htaccess files—it can only be loaded globally in your conf. I don’t have access to this function on Dreamhost, my shared hosting provider, so I needed an alternative solution.

Although rewrite rules are probably the most elegant way to manage this system, YOURLS is written in programmer-friendly PHP. After a little bit of digging around, I decided to modify the yourls-loader.php file to force everything into lowercase.

In yourls-loader.php, immediately after line 14, I added:
$request = strtolower( $request );

This way the initial request processed by YOURLS is immediately flattened to lowercase. Now, no matter what capitalization you throw at it, all that YOURLS will see is lowercase keywords — exactly the kind of insensitivity I wanted.

Related posts

Leave a Reply