CF9 ORM and Apache's mod_rewrite
Here’s another problem I’ve run into with ColdFusion’s ORM (there seem to be a few of these recently…).
I’ve only just started running a CF dev environment under Apache on my Mac – I was previously running IIS under VMWare. The problem comes when using Apache’s mod_rewrite for URL rewriting.
I set up a simple test scenario with the following Application.cfc:
component output="false"
{
this.name = "ormtest";
this.sessionManagement = true;
this.sessiontimeout = CreateTimeSpan(0,5,0,0);
this.ormEnabled = true;
this.datasource = "TEST";
this.ormSettings = {
cfclocation = "/model"
};
}
The model directory contains a single ORM mapping bean, user.cfc.
This runs fine and initialises without a problem. But then I add the following mod_rewrite rules into .htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !/(assets|index.cfm) [NC]
RewriteRule ^(.*)$ /index.cfm/$1 [NC,L]
This handles rewriting of SES URLs for FW/1 pages – essentially, anything that’s not in the assets folder gets index.cfm/ prepended to it behind the scenes. However, this causes the following error when loading the application:
Could not find the ColdFusion component or interface user.
It turns out that the reference to /model as the ORM’s cfclocation is also being rewritten; what is more, adding model into the RewriteCond of paths to ignore doesn’t solve the problem.
After a bit of searching, I discovered Richard Herbert had experienced what seemed to be the same problem. A quick tweet later, and he told me of the solution he’d found.
The solution is simple: create a mapping to the directory containing the ORM CFCs. So, in my example, I added the following before the ORM settings:
this.mappings = {'/model' = '/Volumes/Dev/Web/sites/ormtest/model'};
…and everything now works as it should. (Note: be sure to change the mapping to reflect the structure of your live server!)
Out of interest, I tried the same test under IIS running ISAPI_Rewrite – and it didn’t share the problem. So I assume that mod_rewrite runs at a lower level in the system than ISAPI_Rewrite: mod_rewrite will rewrite your internal CF file calls. So that’s something to be wary of…