If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

I have been a huge fan of Fusebox but one of the things that always bugged me was the ugly URLs it would create. I wanted to be able to have my URLS look pretty like most Ruby On Rails applications and I was wondering how they did it. I started to look around and found that Mod_REwrite was the way they were doing it. Mod_rewrite is an Apache module that allows you to "write" the URLs with Regular Expressions into something that the application server can read. It can take a URL looking like

http://www.mydomain.com/index.cfm?d0=product.list&productID=123452

into

http://www.mydomain.com/product/list/productID/123456.html

It appears to be a nice directory structure with a static HTML page... and here is how!

First off we need to make sure that mod_rewrite is working. You will need to remove a pound(#) since inside of your HTTPD.conf file on this line:

#LoadModule rewrite_module modules/mod_rewrite.so

Restart Apache and then open up your httpd-vhosts.conf file

What you need to do is add a directive to your site called AllowOverride. This tells apache to look for a .htaccess file and process whatever is in it.

<Directory "C:/websites/mysite.com">
AllowOverride All
</Directory>

Put that inbetween your <virtualhost> tags and bounce apache again.

From there we need to create a file called .htaccess . This file will contain the code needed to do the rewriting. Just open up your text editor and put this code in:

CODE:
  1. RewriteEngine on
  2.  
  3. RewriteCond %{REQUEST_FILENAME} !-f
  4.  
  5. RewriteCond %{REQUEST_FILENAME} !-dRewriteRule (.*) /index.cfm?str=$1 [L]

This does a couple of things. The first line turns the engine on. The second and third are telling the module not to do anything if this is a File (!-f) or a Directory (!-d). This will allow images and real files to be served up. The 4th line is taking everything in the URL after the domain name and pass it as a URL variable called "str".

Basically at this point Mod_rewrite has done its job. The next thing we need to do is start to process it in ColdFusion...

First thing we want to do is open up fusebox.init.cfm in our application. In that page we are going to parse the url.str and make it into some variables that coldfusion can handle.

This is the block of code you will need:

CFM:
  1. <!--- URL rewrite --->
  2.  
  3. <cfif not(structKeyExists(url, "do")) and structKeyExists(url, "str")>
  4.  
  5.     <cfif listlen(str,"/") GT 1>
  6.         <cftry>
  7.             <cfset attributes.do = "#ListGetAt(url.str,1,"/")#.#ListGetAt(ListGetAt(url.str,2,"/"),1,".")#">   
  8.         <cfcatch>
  9.         </cfcatch>
  10.         </cftry>
  11.     </cfif>
  12.    
  13.     <cfif listlen(str,"/") GT 2>
  14.         <cfloop from="3" to="#listlen(str,"/")#" step="2" index="idx">
  15.         <cftry> 
  16.             <cfset "attributes.#ListGetAt(url.str,idx,"/")#" = ListGetAt(ListGetAt(url.str,idx + 1,"/"),1,".")>
  17.         <cfcatch>
  18.             <cfset "attributes.#ListGetAt(url.str,idx,"/")#" = "">
  19.         </cfcatch>
  20.         </cftry>
  21.         </cfloop>
  22.     </cfif>
  23.  
  24.  
  25. </cfif>
  26.  
  27. <cfparam name="url.str" default="">

This takes url.str and parses it into the attributes scope. Your URLs end up looking like this:

/circuit/fuseaction/variableName/VariableValue/Variable2name/VariableValue2

Well you get the point. This also takes anything after a period and ignores it so you can have:

/circuit/fuseaction/variablename/variablevalue.html or.php or even .asp :-)

This does take some playing around to get to work for your needs. There are other things to watch out for is that URL encoded stuff sometimes breaks and you have to make sure that you add some stuff to your error handling to handle 404s. If you do this then you can make great SEO URLS that the search engines will LOVE!

Comments

Sean Corfield on 22 November, 2007 at 6:23 pm #

If you’re comfortable with a slightly more orthodox format of:

/index.cfm/do/product.list/productID/123452

then Fusebox 5.5 can handle this completely automatically. Fusebox 5.1 introduced a way to build SES URLs with the XFA verb and Fusebox 5.5 introduces automatic parsing of this form of SES URLs. See the docs for more information.


J.J. Merrick on 22 November, 2007 at 6:35 pm #

Thanks Sean!

I actually haven’t looked at 5.5. I have a friend who keeps IMing me with “dooood 5.5 is SO sweet!” :-)


Radek on 23 February, 2008 at 5:29 pm #

Hi I want to ask, I am new to mod rewrite. I know it work on php, but I am trying to make it work on coldfusion. And site is hosted on windows server. Is there a way to make it SEO friendly like this for example:

http://www.examplesite.com/word.cfm?productoid=23&category=6

to

http://www.examplesite.com/Books/Science

Is there a way in Coldfusion? Thanks


Post a Comment
Name:
Email:
Website:
Comments: