If you check the official documentation on URLRewriting in CFWheels, it’s said that Wheels doesn’t support using URL Rewriting if Wheels is in a subfolder, however there are people out there doing it. I’ve been trying to figure out how to do this by scowering the forums and everything, but just couldn’t find a diffinitive example on how to do it. So as a benefit to myself and others, here’s how to do it under IIS.

Imagine (if you will) we have a website with the following setup:

Directory: c:\inetpub\wwwroot\example
Domain: http://www.example.com

and we want Wheels to run in the subfolder /myapp/survey. So the full url to our Wheels app would be:

http://www.example.com/myapp/survey

The first step is to right click on your website in IIS and click “properties“.
Then in the ISAPI Filters tab, you are going to install the IsapiRewrite4.dll as a filter. So click “Add” and enter in a name for the filter (call it Rewrite for good measure) and browse to the IsapiRewrite4.dll in your application directory. The path should be (if using this tutorial as a guide):

c:\inetpub\wwwroot\example\myapp\survey\IsapiRewrite4.dll

Click “ok” and then “ok” again. IIS is now setup.

Now it’s time to edit the IsapiRewrite4.ini to reflect the path. Open up the IsapiRewrite4.ini in our application folder, it should look like this:

RewriteRule ^(/(flex2gateway|jrunscripts|cfide|cfformgateway|railo-context|files|images|javascripts|miscellaneous|stylesheets).*)$ $1 [L,I]
RewriteRule ^(/.+/.+/.*\?.+\..*)$ /rewrite.cfm/$1
RewriteRule ^(/[^.]*)$ /rewrite.cfm/$1

Now the easy part, edit the rules in the ini file to reflect our application path. Following our example, your IsapiRewrite4.ini should look like this:

RewriteRule ^/myapp/survey(/(flex2gateway|jrunscripts|cfide|cfformgateway|railo-context|files|images|javascripts|miscellaneous|stylesheets).*)$ /myapp/survey/$1 [L,I]
RewriteRule ^/myapp/survey(/.+/.+/.*\?.+\..*)$ /myapp/survey/rewrite.cfm/$1
RewriteRule ^/myapp/survey(/[^.]*)$ /myapp/survey/rewrite.cfm/$1

Notice that we added “/myapp/survey” to the beginning of every rule and just before the “/rewrite.cfm” in rules 2 and 3.

Now the only thing left to do is reload our web application in IIS to reflect the changes. If you’re using IIS 6.1 (Windows 2003), this is really easy. Just expand the “Application Pools” folder in the IIS Administrator and find the Application Pool that corresponds to your Wheels application (hint: right click on your website, select “properties”, and click the “Home Directory” tab. The Application Pool that is assign to your website will be list here). Now just right click on the Application Pool and select “Recycle

That’s it! Now open a browser and point it to:

http://www.example.com/myapp/survey

and you should get the Wheels Congratulations screen. Go luck and leave any comments, corrections or flames below.

6 Responses to “CFWheels: URL Rewriting in a subfolder using IIS”

  1. Daniel Says:

    how can this be done in apache?

  2. rip747 Says:

    you would just have to edit the .htaccess file that comes with cfwheels and put the subfolder path before the rewriterules.

  3. Mike Henke Says:

    Very nice. Thanks

  4. Mike Henke Says:

    You missed one spot for images,stylesheets,etc:

    RewriteRule ^/myapp/survey(/(flex2gateway|jrunscripts|cfide|cfformgateway|railo-context|files|images|javascripts|miscellaneous|stylesheets).*)/myapp/survey$ $1 [L,I]

  5. rip747 Says:

    thanks for catching that mike. updated and fixed.

  6. Mike Henke Says:

    People using .htaccess can accomplish this using:

    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^/myapp/survey/(flex2gateway|jrunscripts|cfide|cfformgateway|railo-context|files|images|javascripts|miscellaneous|stylesheets|rewrite.cfm|index.cfm) [NC]
    RewriteRule ^(.*)$ /myapp/survey/rewrite.cfm/$1


Leave a Reply