Short URLs with IIS and ColdFusion

Since I started my new job, I have been learning ColdFusion, IIS and MSSQL 2000 / 2005. To be completely honest, I prefer PHP and Apache right now over CF and IIS due to a number of issues. Code readability being front and center. I also like the power of Apache over IIS for many, many things. The MSSQL vs MySQL bit is a bit of a wash though, as I am really beginning to like SQL 2005 due to the reporting services. Though this isn't much of a transition for me, there are somethings that are harder to do, learning curve not withstanding.

I almost cringed when I was asked to come up with a solution for SEO on TonerMax due to not being able to work with Apache. Ughh, no mod rewrite... That was my attitude, and I have been putting it off for a bit while I could think of a solution.

While, my code might be hacky due to just learning IIS and CF, I have to admit that it is even easier in IIS to set up short URLs than in Apache (although, I am sure you can do this exact method in Apache). In addition, using the cfheader tag in CF has allowed me to stay within the http specs.

How am I doing this? Custom 404s, my friends. As I said, this method could be used with Apache and PHP, but I am not going to dwell, as I believe mod rewrite is the better solution. That said, this is pretty easy to pull off in about 10 minutes.

There are always two things that you have to worry about when doing short urls. How are you going to encode them, and how are you going to decode them. For me, the first part was pretty easy as my framework isn't very fancy on URL generation. I simply force a 404 via a normal anchor tag. That is, I point my links to something that doesn't exist. Since, what I am pointing to doesn't exist, I can also add verbiage into URL that might make the spiders like my site just a little better. For instance:

Before:
https://www.tonermax.com/extranet/pm.cfm?screen_id=29204&site_id=3&item_id=538

After:
http://www.tonermax.com/EPSON-STYLUS-COLOR-777-INKJET/i/538

The before case is the actual URL of my item. The After case doesn't exist except it looks better for SEO and readability reasons. It really just forces a 404, but for my purposes it also encodes my URL to do what I want.

The decode part is really simple to do in ColdFusion, as much as it pains me to admit. Basically, all I am doing is decoding the query string, looking at the last two elements and then loading in the actual page.

The decode looks something like this:

The first two lines in this decode the the last two elements of my URL. In my example above it is i/538. In actuality, that is the only part of the URL that matters to me. The rest is fluff for everyone else. This part tells me that I am looking at an item, and that item id is 538. Once again, easy enough to do in PHP as well.

However, where I would have to think about how to do this in PHP, is real easy to do in CF. Using the cfhttp tag, I can just load in my page that I need without redirecting, etc. It does add to the processing time of the page, and also chews into the memory a bit, but nothing real noticeable from my tests. It works like a server side IFRAME for lack of better terms.

Easy enough. The final part of my script is just setting the 404 for stuff that really doesn't exist. I use the cfheader tag to change the status response of my pages in those cases. It seems to work good for my purposes, and just proves that CF isn't as useless as I had thought. IIS is though;)

Print | Related


Warning: Invalid argument supplied for foreach() in /home/niceguy/public_html/var/cache/templates/c71f60f688f84ae1cdfd7639a9815a39.php on line 3