CommandBox running Lucee, newb virtual directory problem

I’m building an app on Lucee, run locally via CommandBox during development. The app has sort of SES-style virtual directory URLs, which don’t actually exist in the file system.

For example:

http://myserver/path/to/app/1/1/44/index.cfm?cmd=someController.someMethod

The ‘1/1/44’ part of that is dynamic, based on IDs of objects created in the app. These URLs are handled by Application.cfc’s onRequest() method, and all of that is working well.

Problem happens when someone accesses just that pseudo-directory, http://myserver/path/to/app/1/1/44/, with no file name. Application.cfc doesn’t get called at all, Tomcat just returns a 404. That’s reasonable, the directory doesn’t actually exist, but I’d like to be able to handle this scenario somehow.

Is there any way I can support this?

Thanks.

I would recommend using ColdBox. Our URL routing mechanism is much better :slight_smile:

I can’t really answer you since you haven’t shared any details at all about what your rewrites look like or how you’re handling routes, but I can make a guess. The problem with URLs that don’t have the index.cfm is that they don’t get passed to CF. The fix for this (which is what ColdBox does) is to route ALL URLS THROUGH INDEX.CFM. Here is the default route structure for a ColdBox app:

site.com/index.cfm/my/route/here

The rewrite works like so-- any URL that’s hit that doesn’t map to a “real” file or folder has index.cfm slapped in front of it to force the request through CF and then your CF code pulls the original URL out of the path info and parses it. So the user can hit this:

site.com/my/route/here

And the rewrite rule turns it back into

site.com/index.cfm/my/route/here

index.cfm is the front controller for the framework so all traffic goes through and the rewrite rule ensures that all requests that aren’t to static files like images or js are forced through CF so you always get to handle the request and then you can decide when you wish to throw a 404. (In ColdBox we have an onInvalidEvent handler for this) As far as how you need to do this, I realy can’t help you further since you haven’t shared any details about what you have set up. I can, however, point you to the docs for our URL rewrites in CommandBox and you can put the pieces together.

https://ortus.gitbooks.io/commandbox-documentation/content/embedded_server/url_rewrites.html

https://www.ortussolutions.com/blog/the-12-tips-of-commandbox-christmas-day-10-converting-apache-and-iis-url-rewrites

Thanks!

~Brad

ColdBox/CommandBox Developer Advocate
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Thanks for jumping in Brad, I know I didn’t provide a lot of details.

Much as I respect ColdBox, this is a medium large and moderately complex app, and migrating it to ColdBox is pretty certainly not happening, at least this generation. In its defense, while it isn’t ColdBox, it is a front controller architecture, and pretty well organized. The URLs I’m having problems with aren’t generated by the app, they’d only get hit if a user typed them in etc, I’m just trying to handle that gracefully anyway.

The most interesting part of what you’re saying is that ColdBox handles URLs without a filename, which as we’ve both said, aren’t handed off the CF at all. How does ColdBox get to do anything at all in that case?

Related, by asking about ‘rewrites’, do you mean Apache or other web server rewrite rules? Is that how ColdBox gets its hands on directory-only requests? In production, there probably will be an outside web server, but here in dev under CommandBox, that’s not an option, so I was wondering if there was a CommandBox option or strategy that could accomplish the same thing.

Just so I know, can a web server rewrite rule handle providing a default file when the requested directory doesn’t exist? Since my URLs have the structure I showed, that’s the case most of the time. But again, for now I’m investigating if I can handle this case without a wbe server other than a CommandBox Lucee instance.

> The most interesting part of what you’re saying is that ColdBox handles URLs without a filename, which as we’ve both said, aren’t handed off the CF at all. How does ColdBox get to do anything at all in that case?

ColdBox handles all requests because they all have index.cfm in them. The rewrite rules enforce this. Even if the end user’s browser doesn’t show the index.cfm, the rewrite rule puts it there before it reaches CF. The only other way of doing this is to use a 404 handler that’s a cfm file. It works, but it’s janky IMO

> about ‘rewrites’, do you mean Apache or other web server rewrite rules?

Yes.

> Is that how ColdBox gets its hands on directory-only requests?

Yes. And to be clear, ColdBox has no idea it’s even happening. The URLs are modified on the fly in the web server before they ever hit CF.

> I was wondering if there was a CommandBox option or strategy that could accomplish the same thing.

CommandBox supports rewrites just like IIS or Apache and has since version 1. See the links I pasted in my last E-mail which detail them. There is a default set of rewrites that does the simple addition of index.cfm to your URLs, or you can create your own custom rewrites to do whatever you want.

> Just so I know, can a web server rewrite rule handle providing a default file when the requested directory doesn’t exist?

Yes. That’s what this whole conversation is about.

> I’m investigating if I can handle this case without a wbe server other than a CommandBox Lucee instance.

CommandBox supports all the same rewrites that IIS, Nginx, or Apache supports. Don’t worry about that. In fact, the second link I gave you is a blog post showing how to convert IIS rewrite rules to CommandBox! Apache is even easier, CommandBox can literally just read your .htaccess file as it’s written.

Thanks!

~Brad

ColdBox/CommandBox Developer Advocate
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

OK thanks Brad. The bit I was trying to be sure of was that web server level rewrite rules are needed in this CommandBox context, and how CommandBox can implement them. Slick that it’s compatible with other web servers.

Thanks again,
Dave