Often when we deploy a coldbox website we fire up commandbox, create a webserver and put NGINX in front to route our application requests to the correct lucee instance. We never experienced any problems when configuring search engine safe (SES) url’s. Just to remind you: instead of writing this

https://mysite.nl/index.cfm?event=myhandler.myaction

we can rewrite this to something more friendly such as

https://mysite4u.nl/myhandler/myaction

So we proxy our request via Nginx to a lucee coldbox application and use some rewrite rules as described in the coldbox manual. Recently we had a slightly different configuration: apache in front of an old-fashioned standard lucee standard install on Linux. Again, we followed instructions in the same coldbox manual but NO success…

Whatever I tried I couldn’t get this SES configured in the right way on my Apache box. Many thanks to the amazing Pete Freitag from Foundeo who put me on the right track to solve this problem. I will share it with you here.

In the coldbox manual there is also a .htaccess rewrite example for Apache. I’ll share the most important part here with you

#The ColdBox index.cfm/{path_info} rules.
RewriteRule ^$ index.cfm [QSA,NS]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.cfm?%{REQUEST_URI} [QSA,L,NS]

So what happens here in the last line is that all requests will be sent to a url with path info so my https://mysite4u.nl/myhandler/myaction will be send to https://mysite4u.nl/index.cfm?myhandler/myaction and this will arrive in our coldbox application. Coldbox has a router configuration where it will do some magic to discover the correct handler and action from this index.cfm?myhandler/myaction. This all works fine in Nginx, but in spite of my detailed copy of the instructions it failed in Apache. Finally I discovered why.

The coldbox router needs the CGI.PATH_INFO variable to process your request correctly BUT if you forward your apache requests with ProxyPass or ProxyPassMatch apache will NOT SEND CGI.PATH_INFO to lucee. In older versions of apache this was still OK, but more recent versions of apache it it considered normal that this will not be sent, as it has to conform to some RFC. So the coldbox router doesn’t know what to do, because the path info is not there.

Luckily coldbox has some special feature in the coldbox router, named pathInfoProvider, which can be used to determine the correct path info in this case. Because it might be hard to determine from the url and info in your CGI variables might differ depending on your hosting stack, it makes sense to use the solution as suggested by Pete. You have to create a special .htaccess file for your rewrites which looks like this one.

RewriteEngine On
RewriteRule ^$ index.cfm?redirect_path=/ [QSA,NS]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.cfm?redirect_path=%{REQUEST_URI} [QSA,L,NS]

So you catch your path and send it to index.cfm with a special url variable named redirect_path (or any other name you like) which has the original path in it. Just make sure you use the same url variable name in the second part of your solution. In your router.cfc you create a special pathInfoProvider function which replaces your cgi.path_info with the correct path info from your redirect_path query variable.

function PathInfoProvider( event ){
  var p = cgi.path_info;
  if (len(p)) {
    return p;
  } else if (url.keyExists("redirect_path")) {
    return url.redirect_path;
  } else {
    return "";
  }
}

So if there is a cgi.path_info variable it will use this one (handy for development!). If it is not there (on our production apache box) it will detect the path info from my url variable.

Many thanks to Pete for this workaround. The PathInfoProvider function is fully documented in the manual. I guess it is one of the few parts of coldbox which I never had to use before!