Blog

15
December 2016

Gavin Pickin

CommandBox rewrites are easier and more powerful

CFML Server, CommandBox, Lucee, Techie Gotchas

Using CommandBox as my every day dev server is awesome. I know the pain developers feel, trying to run multiple versions of their server software locally. I have even presented at conferences about how to get your host machine setup to run Railo, Lucee, and ACF 9 through ACF 11 on the same machine, with a lot of headaches, but you can do it. There is a demand for that, and to be honest, CommandBox put me out of a job. Although, as great as CommandBox is, for running multiple servers, a couple of things were not that easy, a couple of things the lightweight built in webserver didn't do as well, mainly REWRITES... until now.

Using Apache for several years now, writing rewrites in Apache's mod_rewrite syntax had spoiled me. Commandbox has supported rewriteConfig from the start essentially, with its own built in config, and more recently, with your on web.rewrites.config file option, but it was not the same. I know a lot of people use Tuckey, and it has a lot of power, I just never felt at home. Having to dive into the XML, and setup the rules, and then use them, honestly, I have just been spoiled.

Just recently Brad Wood had mentioned, there was some undocumented, and largely untested rewrites functionality that I might be interested in. He was right, mod_rewrite syntax is now supported in CommandBox… awesome… but now we have to figure out how it works.

Long story short, I dove into it one day, and got to the bottom of it, and I'm about to tell you all about it.

To be honest, the hardest part about getting it working, was that unlike the xml styled rewrites, which can take any file name and work, mod_rewrite required the '.htaccess' name for the file.

So lets get started. Open up CommandBox, at your root folder.
Instead of using the following 2 lines

server set web.rewrites.enable=true
server set web.rewrites.config=customRewrites.xml

 

You should use

 

server set web.rewrites.enable=true
server set web.rewrites.config=.htaccess

 

You can confirm your settings with the server show command at the CommandBox cli.

 

server show web.rewrites.enable
server show web.rewrites.config

 

Lets create a `.htacess` file.
Lets use this sample file.

 

RewriteEngine on
RewriteRule ^/foo/ /

# redirect mozilla to another area
RewriteCond %{HTTP_USER_AGENT} ^Mozilla.*
RewriteRule ^/no-moz-here$ /homepage.max.html [L]

 

Save your file.
If your server is running already, use

 

server restart

 

If it's not running, use

server start

 

Test out your rewrites... hope they work out as good for you, as they did for me.

More information:

Read the CommandBox Rewrite documentation here - https://ortus.gitbooks.io/commandbox-documentation/content/embedded_server/url_rewrites.html

Read the blog post about using Tuckey rewrites with CommandBox here - https://www.ortussolutions.com/blog/commandbox-and-url-rewrites-made-easy

Read the Tuckey docs here - http://cdn.rawgit.com/paultuckey/urlrewritefilter/master/src/doc/manual/4.0/index.html#configuration

 

Blog Search