URL Rewriting
Making URLs prettier using URL rewriting.
URL rewriting is a completely optional feature of Wheels, and all it does is get rid of the index.cfm part of the URL.
For example, with no URL rewriting, a URL in your application could look like this:
http://localhost/index.cfm/blog/new
After turning on URL rewriting, it would look like this:
http://localhost/blog/new
Combine this with the routing functionality of Wheels, and you get the capablility of creating some really human-friendly (easier to remember, say over the phone, etc.) and search engine friendly (easier to crawl, higher PageRank, etc.) URLs.
Once you have uncommented the rewrite rules (found in either .htaccess, web.config or IsapiRewrite4.ini), Wheels will try and determine if your web server is capable of rewriting URLs and turn it on for you automatically. Depending on what web server you have and what folder you run Wheels from, you may need to tweak things a little though. Follow these instructions below for details on how to set up your web server and customize the rewrite rules when necessary.
Instructions for Apache
On most Apache setups, you don't have to do anything at all to get URL rewriting to work. Just uncomment the rewrite rules in the .htaccess file and Apache will pick up and use them automatically on server start-up.
There are some exceptions though...
If you have installed Apache yourself you may need to turn on the rewrite module and/or change the security settings before URL rewriting will work:
- Check that the Apache
rewrite_modulehas been loaded by ensuring there are no pound signs before the line that saysLoadModule rewrite_module modules/mod_rewrite.soin the httpd.conf file. - Make sure that Apache has permission to load the rewrite rules from the .htaccess file. This is done by setting
AllowOverridetoAllunder theDirectorysection corresponding to the website you plan on using Wheels on (still inside the httpd.conf file).
If you have an older version of Apache and you're trying to run your Wheels site in a sub folder of an existing site you may need to hard code the name of this folder in your rewrite rules.
- Change the last line of the .htaccess file to the following:
RewriteRule ^(.*)$ /sub_folder_name_goes_here/rewrite.cfm/$1 [L]. Don't forget to changesub_folder_name_goes_hereto the actual folder name first of course.
Instructions for IIS 7
Similar to Apache, IIS 7 will pick up the rewrite rules from a file located in the Wheels installation. In the case of IIS 7, the rules are picked up from the web.config file. (Don't forget to uncomment the XML block containing the rewrite rules in that file first.)
This requires that the URL Rewrite Module is installed. It's an IIS extension from Microsoft that you can download for free.
Instructions for IIS 6
Unfortunately, there is no built-in URL rewriting mechanism in IIS 6, so getting Wheels working with pretty URLs is a little more complicated than with Apache and IIS 7 (which often comes with the official "URL Rewrite Module" installed by default). Here's what you need to do:
- Download Ionic's ISAPI Rewrite Filter. NOTE: the version must be v1.2.16 or later.
- Unzip the file, get the IsapiRewrite4.dll file from the lib folder and put it in the root of your website. (It needs to be in the same folder as the IsapiRewrite4.ini file.)
- To enable the rewrite filter in IIS 6, click on Properties for your website, then go to the ISAPI Filters tab and click the Add... button.
- Type in anything you want as the Filter Name and point the Executable to the IsapiRewrite4.dll file.
- Uncomment the rewrite rules in the IsapiRewrite4.ini file.
Deleting Unnecessary Files
The sole purpose of the .htaccess (for Apache), web.config (for IIS 7), and IsapiRewrite4.ini (for IIS 6) files is to make it possible to use URL rewriting.
Don't Forget to Restart
If you need to make changes to get URL rewriting to work, it's important to remember to always restart the web server and the ColdFusion server to make sure the changes are picked up by Wheels.
If you don't have access to restart services on your server, you can issue a reload=true request. It's often enough.

Comments
Read and submit questions, clarifications, and corrections about this chapter.
[Add Comment]
Thank you for the link to download ISAPI filter. After watching the blawgh video I started searching for an URL rewrite utility for IIS but only found a tool which cost over $99. Good thing somebody has posted the above URL.
Best
Sam
# Posted by ColdFusion Developer | 11/9/2008
I installed IIS7 and downloaded a simple install for URL Rewrite by Microsoft from http://www.iis.net/1692/ItemPermalink.ashx
If you too are running IIS7, you can you use my web.config file and place it to the root of your site after you have installed the utility.
The "web.config" file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="CFWheels Rule>
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{SCRIPT_NAME}" negate="true" pattern="^/(flex2gateway|jrunscripts|cfide|cfformgateway|railo-context|files|images|javascripts|miscellaneous|stylesheets|rewrite.cfm|index.cfm)" />
</conditions>
<action type="Rewrite" url="/rewrite.cfm/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
This works great and amazingly cool.
Best
Sam
# Posted by ColdFusion Developer | 11/22/2008
I m using cold fusion built in server, I need to want to turn on URL rewriting, how i will do this. pls suggest me.
thanks
# Posted by Pushpa Gupta | 12/25/2008
I installed CFWheels on Mac OS X 10.4 using Apache 1.3 server. I added the LoadModule rewrite_module modules/mod_rewrite.so in the httpd.conf file and changed the AllowOverride to All. But when I try to do the HelloWorld tutorial and enter http://localhost:8500/WheelsSite/say/hello, I get a 404 not found error.
Please help.
# Posted by tsqware | 6/21/2009
For URL rewriting to work the website needs to be at the root, i.e. http://localhost:8500/say/hello instead of http://localhost:8500/WheelsSite/say/hello.
# Posted by Per | 6/21/2009
*strange* i'm on an ubuntu (8.04) with apache2 and url_rewriting enabled. i can call all pages directly with
http://mysite.local/say/hello even deleting and adding work, but when i use linkto i and up with mysite.local/rewrite.cfm?controller=.... any guess about this?
thx!
# Posted by reinhard | 6/29/2009
Not sure, Reinhard but you can force Wheels to use a specific type of URL rewriting by adding set(URLRewriting="On") to your configuration files.
# Posted by Per | 6/29/2009
thx per ... that did the trick ;)
# Posted by reinhard | 6/29/2009
This is what I have for Railo on Resin
<host id="cfwheel.webonix.org">
<root-directory>./cfwheel.webonix.org</root-directory>
<web-app id="/" document-directory="webroot"/>
<rewrite-dispatch>
<forward regexp="^(/(flex2gateway|jrunscripts|cfide|cfformgateway|railo-context|files|images|javascripts|miscellaneous|stylesheets).*)$" target="$1" />
<forward regexp="^(/.+/.+/.*\?.+\..*)$" target="/rewrite.cfm/$1" />
<forward regexp="^(/[^.]*)$" target="/rewrite.cfm/$1" />
</rewrite-dispatch>
</host>
So far it seems to be working fine
# Posted by AJ Mercer | 8/20/2009
In order for this to work for me, on my development environment (OSX Leopard), I had to add the option to enable symlinks in the directory in my virtualhost:
<VirtualHost *:80>
ServerName cfwheels.local
DocumentRoot "/blah/blah/cfwheels"
<directory "/blah/blah/cfwheels">
AllowOverride All
Options FollowSymLinks
</directory>
</VirtualHost>
If I didn't have followsumlinks there, it would constantly give me a 403 forbidden when I tried to hit a controller without index.cfm.
Hope this helps somebody!
Groove
# Posted by Groovetrain | 9/13/2009
URL ReWriting won't work for me on Win2k3/IIS6 but works fine on Win7/IIS7. Any suggestion, I followed the instruction and I'm using the supplied .ini file which of course I uncommented the included rules. Restarted CF & IIS without success.
# Posted by voluzion | 11/12/2009
I had to modify the .htaccess file to make it work with the Helicon ISAPI Rewrite Manager. Here's what I ended up with:
#This works for Helicon ISAPI Rewrite Manager
RewriteRule (^/(flex2gateway|jrunscripts|cfide|cfformgateway|railo-context|files|images|javascripts|miscellaneous|stylesheets|robots.txt|sitemap.xml|rewrite.cfm)($|/.*$)) $1 [L,I]
RewriteRule ^/(.*)$ /rewrite.cfm/$1 [L]
# Posted by Clarke Bishop | 11/30/2009
Using IIS7 and CF9 it took me a while to figure out how to make URL rewriting work.
In the description above, it is mentioned that Wheels turns on URL rewriting automatically when it finds your server is capable. Apparently, that doesn't always work correctly.
If rewriting isn't working on IIS and you checked all the basics, you can go to the IIS7 management console and choose the correct site for your Wheels application. Under the heading IIS, you find a button called 'URL Rewrite'. Double click this button, and under 'state' you see if rewriting is 'disabled' or 'enabled'. From here on it's pretty self explanatory: if it's disabled, right click on the rule and choose to enable it.
After puzzling for quite some time as to what could be the problem, this solved it for me. No clue why Wheels 1.0 isn't able to turn it on by default on my particular server.
# Posted by Andor Admiraal | 1/2/2010
I'm in the same boat with Pushpa. Any way to make rewriting work with the built-in dev server?
# Posted by Keith | 2/20/2010