CakePHP 2.0 Plugin Webroot URL Rewrite on IIS 7

After trying my hand at CakePHP and falling in techno-geek love with it, I ran into a problem with how CakePHP 2.0 Beta runs it’s plugins’ webroot content. Just so we’re on the same page, the 2.0 documentation hasn’t been updated as of writing (Aug 14, 2011) to include any changes to how plugin’s webroot work … so I’m assuming it’s pretty much the same as 1.3. The docs for 1.3 state:

The urls to plugin assets remains the same. In the past you used /debug_kit/js/my_file.js to link toapp/plugins/debug_kit/vendors/js/my_file.js. It now links to app/plugins/debug_kit/webroot/js/my_file.js

Perhaps this works in Apache, but I’m running IIS7 and it does not work for me. In my layout (or view, whichever) I tried inserting the following code:


The file was not found and generated a URL exactly as it was written above, which caused a “Controller Manager not found” error. After some digging I found this issue which seemed to point that the problem was related to URL rewriting. If the person disabled mod_rewrite on Apache things started to break down. So I took a look at the web.config file obtained from the Cake docs and lo-and-behold it didn’t really cater towards Plugins.

So I updated my web.config file in the root directory to read as follows:

	<rule name="Redirect plugin resources" stopProcessing="true">
	    <match url="^(.*)/(ico|img|css|files|js)(.*)$" />
	    <action type="Rewrite" url="app/plugin/{R:1}/webroot/{R:2}{R:3}" appendQueryString="false" />
	<rule name="Redirect static resources" stopProcessing="true">
	    <match url="^(ico|img|css|files|js)(.*)$" />
	    <action type="Rewrite" url="app/webroot/{R:1}{R:2}" appendQueryString="false" />

The rule “Redirect plugin resources” is new, the other one is there for reference. Basically I said that if there is any URL containing ico/img/css/files/js that also has a prefix (like ‘/manager/’) redirect this request to /app/plugin/{PLUGIN NAME GETS PUT HERE}/webroot/{ico/css/whatever}/{filenamehere}.

Works like a charm and doesn’t break the other functionality for other webroot files. Give it a try and hopefully this will help you out!

Happy baking,