Technology, The Web, and Oxford Commas.

by Chris Mallinson


URL Trimming in Mango Blog

Instead of using all the URL shortening services out there, I thought it would be just as easy to write my own, and retain my own domain in the URL. My domain name is not that short, but since I would only be using it to redirect to my own pages, I would only need a few characters to create the links. I use Apache as a webserver, and Mango Blog as an engine. This method, however, also works with BlogCFC, another popular, and awesome, ColdFusion blog engine. Both these blog engines use UUIDs as primary keys in the blog entry tables. I added an integer field in the entry table called “pk”, and made it the primary key with auto-increment turned on. This gives all your blog entries a numeric index. The next step is to create a file somewhere on your site with some code to redirect requests that use your URL shortening service. I called it trimURL.cfm:

1
2
3
4
5
6
7
8
9
10
11
<cfquery name="getURL" dataSource="mallinson">
  SELECT    name
  FROM 	 entry
  WHERE	 pk = <cfqueryparam CFSQLType="CF_SQL_VARCHAR" value="#url.id#" />
</cfquery>
 
<cfif getURL.recordcount>
	<cflocation addToken="no" url="http://mallinson.ca/post/#getURL.name#" />
<cfelse>
	<cflocation addToken="no" url="http://mallinson.ca" />
</cfif>

With this code, this blog entry can be referenced by visiting http://mallinson.ca/12. The next step requires that you know a bit about Apache Web Server, or are familiar with URL rewriting using another type of web server. I use Apache, and URL Rewrite. In my .htaccess file, I have a lot of entries that clean up my URLs, but I’ll isolate the one rule that makes the URL rewriting work.

RewriteEngine On
RewriteRule ^([0-9]{1,6})$ trimURL.cfm?id=$1 [L]

What this bit of code does is take any URL that is composed entirely of digits, up to 6 digits (I don’t anticipate breaking a million posts), isolate the digit, and pass it to my redirecting page. I intend to register another much shorter domain as soon as I find something good. Then I could get the URL strings down to 10 characters or so.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>