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 URL rewriting off, 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 URLs (easier to crawl, higher PageRank, etc.).

Unfortunately, if you're running your Wheels site in a subfolder of another site, you will not be able to turn on URL rewriting (at least not without making some custom changes to your rewrite rules), so we recommend running the Wheels site as a stand-alone site whenever possible. If you've met this one requirement and want the shortest and prettiest URLs possible, then just follow the instructions below.

Instructions for Apache

  1. Check that the Apache rewrite_module has been loaded by ensuring there is no pound sign before the line that says LoadModule rewrite_module modules/mod_rewrite.so in the httpd.conf file.
  2. Make sure that Apache has permission to load the rewrite rules from the .htaccess file in the web root. This is done by setting AllowOverride to All under the Directory section corresponding to the website you plan on using Wheels on (still inside the httpd.conf file).

That should be it, in fact, on some Apache setups you don't have to do anything at all. Apache will pick up and use the rewrite rules specified in the .htaccess file on server start-up.

Instructions for IIS

Unfortunately, there is no built-in URL rewriting mechanism in IIS, so getting Wheels working with pretty URLs is a little more complicated than with Apache. Here's what you do:

  1. Download Ionic's ISAPI Rewrite Filter.
  2. Unzip the file and put the .dll file in the root of your website. (It needs to be in the same folder as the IsapiRewrite4.ini file.)
  3. To enable the rewrite filter in IIS, click on Properties for your website, then go to the ISAPI Filters tab and click the Add... button.
  4. Type in anything you want as the Filter Name and point the Executable to the IsapiRewrite4.dll file.

Now restart your web server and enjoy those fancy new URLs. :)


Comments

Read and submit questions, clarifications, and corrections about this chapter.

[Add Comment]

ColdFusion Developer's Gravatar

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

ColdFusion Developer's Gravatar

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

Pushpa Gupta's Gravatar
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

tsqware's Gravatar
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

Per's Gravatar
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

reinhard's Gravatar
*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

Per's Gravatar
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

reinhard's Gravatar
thx per ... that did the trick ;)

# Posted by reinhard | 6/29/2009

Add Comment

Type the two words: