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.
UrlRewriteFilter org.tuckey.web.filters.urlrewrite.UrlRewriteFilter UrlRewriteFilter /*
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:
/flex2gateway/* /jrunscripts/* /cfide/* /files/* /flash/* /images/* /javascripts/* /miscellaneous/* /stylesheets/* /railo-context/* /robots.txt /sitemap.xml /404.shtml /favicon.ico ^(.*)$ /rewrite.cfm?\$pathinfo=$1&%{query-string}
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
- Edit a single core file in Wheels
The first step is super-duper easy. Just add the following line in your config/settings.cfm file:
The second step is also easy but it’s important to remember you need to do this everytime you update Wheels (I checked that the current version, 1.1.5 at the time of writing, still requires this edit).
- Open wheels/dispatch/request.cfm.
- Locate the $getPathFromRequest method (it’s in a different spot in earlier versions of Wheels but in 1.1.5 you can find it at line 120)
- Edit this method so that it goes from the first code snippet to the second.
First:
var returnValue = ""; // we want the path without the leading "/" so this is why we do some checking here if (arguments.pathInfo == arguments.scriptName || arguments.pathInfo == "/" || arguments.pathInfo == "") returnValue = ""; else returnValue = Right(arguments.pathInfo, Len(arguments.pathInfo)-1);
Second:
var returnValue = ""; // ensure that the $pathinfo var is picked up from the URL struct when using URLRewriting via Tomcat/URLRewriteFilter if (StructKeyExists(url, "$pathinfo")) arguments.pathInfo = url.$pathinfo; // we want the path without the leading "/" so this is why we do some checking here if (arguments.pathInfo == arguments.scriptName || arguments.pathInfo == "/" || arguments.pathInfo == "") returnValue = ""; else returnValue = Right(arguments.pathInfo, Len(arguments.pathInfo)-1);
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.