How to have a short id in a link (Coldfusion)

171 Views Asked by At

I am trying to build our own internal URL shortening service since show many of the main ones are being blocked as spam (we use bit.ly now).

What I would like to do is have a URL with an id attached to it which hits a page, looks up the long URL and then forward it on to it. I'm good with everything but the first part.

Here is the kind of URL I would like to send: http://acbd.us/1234567 or http://acbd.us?1234567 where 1234567 is the id we need to look up. I have seen this with other sites but just have no idea how to go about it without referencing a file (lookup.cfm?id=1234567). The whole point is to make it as short as possible.

I have been searching Stack and elsewhere and can'y find a thing.

Thank you and any feedback would be greatly appreciated.

2

There are 2 best solutions below

3
Blomqen On

There are pleanty of free self-hosting options of URL Shorteners if you can't figure out how to make your own.

https://github.com/awesome-selfhosted/awesome-selfhosted#url-shorteners

1
James A Mohler On

The http://acbd.us?1234567 is somewhat easier to implement.

  1. Web servers such as IIS can be made to automatically associate this with a default document. so it becomes

    http://acbd.us/index.cfm?1234567

  2. While we often associate the part after the ? as defining the url variables, it is also a part of cgi.query_string

So based on that you could do something like

index.cfm

<cfscript>
    newloc = QueryExecute("
      SELECT newloc
      FROM lookup
      WHERE shortversion = :shortversion
      ",
      {shortversion = { value : cgi.query_string, CFSQLType : 'CF_SQL_varchar'}},
      { cachedwithin : CreateTimespan(1,0,0,0)}
      ).newloc;

    location(newloc, false);
</cfscript>

We want to cache it for a day because

  1. It should never change and
  2. It needs to be base and
  3. It is a very small bit of data