Coldbox rest handler routing

Hi,
I am new to Coldbox. I tried to find what I am looking for in the old community post but didn’t find any satisfactory resolution, so I am posting a new topic (sorry if the topic seems duplicate).
I am using CF2021 and Coldbox 7.0.
I have created a rest webservice using commandbox (coldbox create app name=myapp skeleton=rest) inside wwwroot/application/v6 folder.
I have added below block in my httpd.conf file

<VirtualHost *:80>
	ServerName api.com
	DocumentRoot "C:/ColdFusion2021/cfusion/wwwroot/application/v6"
	
	# Where to find uriworkermap.properties
	JkMountFile "C:/ColdFusion2021/config/wsconfig/1/uriworkermap.properties"
	#JkMountCopy "All"
	# Where to put jk logs
	JkLogFile "C:/ColdFusion2021/config/wsconfig/1/mod_jk.log"


	<Directory "C:/ColdFusion2021/cfusion/wwwroot/application/v6">
		Options Indexes FollowSymLinks
		AllowOverride None
		Require all granted	
	</Directory>
 
</VirtualHost>

In postman when I hit the url http://api.com/ , I get response from default handler. But for other route which were added like /healthcheck I am getting 404. The url I used in postman is http://restapi.com/healthcheck
This is the code in Router.cfc

route( "/healthcheck", function( event, rc, prc ){
	return "Ok!";
} );

But I am able to get the response in postman with this url http://restapi.com/index.cfm/healthcheck
I also have setFullRewrites( true ) inside the configure function in my Router.cfc. Doesn’t it eliminate the need to add index.cfm in the url. Please correct me if I am wrong. Also, is there anyway I can use http://restapi.com/healthcheck instead of http://restapi.com/index.cfm/healthcheck

Any advice is really apreciated.

Hey there @panda007. Welcome to the community and to Coldbox!

I am not familiar with your particular web server or httpd.conf files, BUT to me, it looks like you may need to add a rewrite rule on your web server to automatically insert index.cfm to requests when the file or directory is missing.

I would take a look at this page and see if there are instructions for your web server:

Within IIS (which is what I use), this change is accomplished with this code in the web.config file:

<!--- if the file or directory doesn't exist, have Coldbox attempt to resolve it --->
<rule name="Insert index.cfm" stopProcessing="true">
	<match url="^(.*)$" ignoreCase="false" />
	<conditions logicalGrouping="MatchAll">
		<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
		<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
	</conditions>
	<action type="Rewrite" url="index.cfm/{PATH_INFO}" appendQueryString="true" />
</rule>

Without this special rewrite rule I would need to insert index.cfm to all of my requests, like what you mentioned in your original post.

I hope this helps!

I ran your question through ChatGPT and it looks like you’re using Apache. So maybe this information will help. If not, double-check my previous link to the documentation because it does mention how to configure Apache settings:

Instructions:

  1. Ensure that mod_rewrite is enabled in your Apache configuration.
  2. Add the rewrite rules inside a <Directory> block or within a .htaccess file if AllowOverride All is enabled.

Here is the updated httpd.conf with the translated rewrite rule:

<VirtualHost *:80>
	ServerName api.com
	DocumentRoot "C:/ColdFusion2021/cfusion/wwwroot/application/v6"
	
	# Where to find uriworkermap.properties
	JkMountFile "C:/ColdFusion2021/config/wsconfig/1/uriworkermap.properties"
	#JkMountCopy "All"
	# Where to put jk logs
	JkLogFile "C:/ColdFusion2021/config/wsconfig/1/mod_jk.log"

	<Directory "C:/ColdFusion2021/cfusion/wwwroot/application/v6">
		Options Indexes FollowSymLinks
		AllowOverride None
		Require all granted	

		# Enable mod_rewrite for URL rewriting
		RewriteEngine On
		
		# If the request is not a file and not a directory, rewrite the URL to index.cfm/{PATH_INFO}
		RewriteCond %{REQUEST_FILENAME} !-f
		RewriteCond %{REQUEST_FILENAME} !-d
		RewriteRule ^(.*)$ /index.cfm/$1 [QSA,L]
	</Directory>
 
</VirtualHost>

Explanation:

  • RewriteEngine On: Enables the URL rewriting engine.
  • RewriteCond %{REQUEST_FILENAME} !-f: Checks if the requested file does not exist.
  • RewriteCond %{REQUEST_FILENAME} !-d: Checks if the requested directory does not exist.
  • RewriteRule ^(.*)$ /index.cfm/$1 [QSA,L]: If both conditions are true, the URL is rewritten to index.cfm/{PATH_INFO}. The [QSA] flag ensures the query string is appended to the new URL, and [L] stops further processing of other rules.

You can also modify the AllowOverride None to AllowOverride All if you prefer using .htaccess files instead. Let me know if you need help with that setup.

Hi Dave,

I had tried the edits suggested by ChatGPT but that didn’t help much. But I did find the solution from the documentation. I had to tweak the rules a bit. This is what I added in the httpd.conf file

<VirtualHost *:80>
	ServerName restapi.com
	DocumentRoot "C:/ColdFusion2021/cfusion/wwwroot/application/v6"
	
	# Where to find uriworkermap.properties
	JkMountFile "C:/ColdFusion2021/config/wsconfig/1/uriworkermap.properties"
	#JkMountCopy "All"
	# Where to put jk logs
	JkLogFile "C:/ColdFusion2021/config/wsconfig/1/mod_jk.log"


	<Directory "C:/ColdFusion2021/cfusion/wwwroot/application/v6">
		Options Indexes FollowSymLinks
		AllowOverride None
		Require all granted
	</Directory>
	
	#The ColdBox index.cfm/{path_info} rules.
	RewriteEngine On
	RewriteRule ^$ /index.cfm?redirect_path=/ [QSA,NS]
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule ^(.*)$ /index.cfm/%{REQUEST_URI} [PT,QSA,L,NS]
 
</VirtualHost>

After adding this I was able to access the URIs configured in Router.cfc without adding index.cfm in url. I didn’t even have to add .htaccess file in my project folder.
Thank you so much for providing the link to Rewrite Rules Documentation.

1 Like

I’m glad the documentation helped, and sorry for the bad ChatGPT response. Thank you for posting the config that eventually worked as this might help someone else in the future.

Happy coding!