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:
After turning on URL rewriting, it would look like this:
Combine this with the routing functionality of Wheels, and you get the capability 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
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.
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 says
LoadModule rewrite_module modules/mod_rewrite.soin the
- Make sure that Apache has permission to load the rewrite rules from the
.htaccessfile. This is done by setting
Allunder the Directory section corresponding to the website you plan on using Wheels on (still inside the
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
.htaccessfile to the following:
RewriteRule ^(.*)$ /sub_folder_name_goes_here/rewrite.cfm/$1 [L]. Don't forget to change
sub_folder_name_goes_hereto the actual folder name first of course.
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.
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.dllfile from the lib folder and put it in the root of your website. (It needs to be in the same folder as the
- 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
- Uncomment the rewrite rules in the
NOTE: Make sure you have "Verify that file exists" disabled for your site.
- Right click your website and select Properties.
- Click Home Directory tab.
- Click the Configuration button.
- Under the Wildcard application mapping section, double-click path for the
- Uncheck Verify that file exists.
- Click OK until all property screens are closed.
UrlRewriteFilter (commonly referred to as Tuckey) is a Java web filter for compliant web application servers such as Tomcat, Jetty, Resin and JBoss. Unfortunately UrlRewriteFilter depends on XML with its extremely strict syntax.
- First follow the (install instructions on the UrlRewriteFilter website).
- Append the servlet-mapping markup to the end of the
<filter mapping>element in your
- Add the pretty urls rule markup to the
<urlrewrite>element to your
- Restart the web application server.
<servlet-mapping> <servlet-name>CFMLServlet</servlet-name> <url-pattern>/rewrite.cfm/*</url-pattern> </servlet-mapping>
Example markup with UrlRewriteFilter and Wheels pretty URLs for
<filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping> <servlet-mapping> <servlet-name>CFMLServlet</servlet-name> <url-pattern>/rewrite.cfm/*</url-pattern> </servlet-mapping>
Pretty URLs Rule markup
A complete barebones
WEB-INF/urlrewrite.xml configuration example with pretty URLs.
Wheels runs just fine using CommandBox; In your wheels webroot, when starting the commandBox local server, add the
--rewritesEnable argument, so your entire string looks like:
$box server start --rewritesEnable
Lastly, you may need to add
config/settings.cfm file, to allow
urlFor() to pickup the appropriate rule.
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. You can delete the files you don't need if you want - just bear in mind they might be useful to others if you have an application with more than one developer.
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.