self.is_a? Blog

random tech drivelings from ouray, colorado

CFWheels and SES URLs

I’ve been talking with a fellow CF'er and he was having some trouble getting SES URLs rolling in a Tomcat and CFWheels application that he’s working on. Since I had to go through all sorts of ‘fun’ times with SES URLs and Wheels a year ago, I thought I’d write up a little post on my experiences and how I got the URLs I want. And, really, it ain’t that bad.

URLRewriteFilter

First things first, download the excellent Java Web Filter library, URLRewriteFilter. Once downloaded, expand the Zip Archive. You will see a WEB-INF folder, inside of which, are two important items: urlrewrite.xml and lib/urlrewrite-3.2.0.jar. From here, it’s not too difficult to figure out what’s next … move these items (the XML and JAR) into the corresponding locations in your application’s WEB-INF folder. Note that you may need to create a lib folder in your WEB-INF (I did).

WEB.XML

Next up, you need to add a declaration to your web.xml file (also located in WEB-INF) so that your server knows to load the URLRewriteFilter JAR. The addition is pretty simple.

Now the filter will be loaded when your server starts.

URLREWRITE.XML

Next, you need to instruct the URLRewriteFilter how to filter and pass on your requests, which is done by providing the conditions under which the filter should not run (in this case). For Wheels, there were two levels at which I did not want the filter to kick in:

  • when certain folders were requested
  • when specific files were requested

For organization and readability, I organized my filter declarations by folders and then files. As you can see from the XML document belowm I select all the major folders where your CFML server is doing stuff (e.g., flex2gateway or railo-context) or where static files are server (e.g., javascripts, stylesheets).

Once your conditions are in place, you need to just tell the filter from what and to which URLs should it rewrite. For Wheels, this appears as follows:

CFWheels

The last piece of the puzzle … the framework itself. There are two (easy) steps to making URL rewrites happen in Wheels:

  • Tell Wheels to turn on URLRewriting
  • Add a piece of code to events/onrequeststart.cfm for populating the path info**

The first step is super-duper easy. Just add the following line in your config/settings.cfm file:

set(URLRewriting="On")

The second step is also easy. Add the following to events/onrequeststart.cfm:

if(structKeyExists(url,"$pathinfo")){
    request.cgi.path_info = url.$pathinfo
}

**EDIT: Many thanks to Tony Petruzzi for pointing out this far more elegant solution.

That’s it. From here, restart your CFML server and you’re ready to roll. To see my SES URLs in action, head to Ouray Climbing and start clicking some links!

P.S. I did write this post rather quickly, so let me know if you spot any errors or have any issues trying the same thing.