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!

Nov
22
Filed Under (Personal) by J.J. Merrick

TurkeyHappy Thanksgiving to all my readers! I am thankful this year for my wonderful family and to be able to do what I love.

Nov
20

A question was posed on the cf-newbie list asking about how to format the first record in a recordset via CSS.

Hi All -

I am producing a front page of a site where I need to have the latest news item and blog entry formatted (via CSS) differently than the later entries. The first record of the news items and blog entry will be formatted with background color and larger font where the later news items and entries will be plain. I believe it has something to do with counting the records in the query, so I've tried something like:

<cfquery name="getNewsItems" datasource="myDSN">
SELECT TOP 5 headline, releaseDate, id
FROM NewsItems
Order by releaseDate
</cfquery>

<h2>News</h2>
<cfif getNewsItems.RecordCount is "1">
<p class="news_events_feature">
<cfoutput query="getNewsItems">
<img src="img/hm/news_events/#id#

.jpg" width="85" height="96" />
#headline#<br />
<a href="##" class="right"></cfoutput>Read More >></a>
</p>

<cfelseif getNewsItems.RecordCount is not "1">
<ol>
<li><cfoutput query="getNewsItems"><a href="##">#headline#<br /><br /></a></cfoutput></li>
</ol>
<a href="##" class="more">More News & Events>></a>
</cfif>

Can anyone tell me why my thinking is not correct?

Thanks,
Adam

A RecordCount gives you the number of records in the query. What you need to use is

queryname.currentRow

and since it is a number just do

<cfif getNewsItems.currentRow EQ 1>

<cfelse>

</cfif>
Easy as pie, your queryname.currentrow will tell you what you row number you are on. Another cool thing is to use MOD to decide if it is every other row to do a grey/white pattern... but that is another blog post!

Nov
19
Filed Under (ColdFusion) by J.J. Merrick

Just decided to install a new design... I wanted the 3 column look and now i got it!

So I came across an issue today where I needed to make sure that a sub directory off one of our domains only went to 1 server behind the load balancer. Basically we had installed wordpress and since it doesn't balance very nicely it only needed to be installed on 1 server and the rest of the domain to act normally. The blog resides in a directory called /blog.

We use a program called Pound to do our load balancing. It is open source and can make any server into a load balancer. I love it because it can do SSL and reverse proxy.

It is included with most ports packages and I won't go into the install here. Basically it uses regular expressions to send a connection to a backend server based on rules. It is totally powered by a single config file. Here is the syntax you will need in that config file...


ListenHTTP
  Address 192.168.5.20
  Port    80

Service
 Url         ".*/(blog|forum)*"
 BackEnd
    Address  192.168.5.105
     Port  80
 End
End

  Service
    BackEnd
      Address 192.168.5.104
      Port    80
    End
    BackEnd
      Address 192.168.5.105
      Port    80
    End
  End
End

That's it! See how easy that was?!

Nov
14
Filed Under (ColdFusion) by J.J. Merrick

I am esteemed member number #23

http://www.coldfusioncommunity.org/profile/JeremiahX

Everybody's doing it... why are you?!

This is more for the "filed away for my own googling later down the road"...

A funny thing happened to me the other day. I had just deployed some new code changes and since it had some application variable changes I needed to bounce ColdFusion server. Much to my dismay CF throws and error and won't come up. OH NO!

The event viewer wasn't much help and just said to see the applications logs and it might help you out.

I started to look around and found a log file that CF was outputting that seemed to alude to the fact that the JVM heap file didn't have enough room to go with the setting that it had.

I then remembered that a couple of weeks ago I had done some work on that instance and played around with the JVM heap settings. I guess I never bounced CF so the settings never too affect... until now!

Well the administrator was not coming up since of course it is written in ColdFusion so I started looking around google and found this.

http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_19359&sliceId=2

Though it says that you can't set it to higher then 1.8 gigs what it should also say is that if JVM can't grab that FULL memory block it ain't running at all! This server had 2 gigs on it so I set it to 1500 since all it is doing is apache and CF. I guess that windows was using more then 500 megs and so Java could'nt get the block it wanted.

I was able to edit the config file the KB mentions and off we went.

So for next time, make sure you bounce after all config changes and make sure you don't set the JVM heap too high!

J.J.

Nov
12

My day job consists of me working for a company called Otis Technologies. We are a small web software shop inside a larger parent company and we were started as the division that would come up with technologies to help the other companies get things done.

The parent company deals with mostly the retail construction space so we were asked to come up with a system that would help them do things, "better, faster cheaper". Well this product started to get used a lot and one of the major things was that clients for the other companies could login to their projects and see what was going on. A lot of them loved it so much that they wondered how they could get their hands on it.. thus SpotOn! was born.

I really enjoy working on "consumer" apps. There is just something about creating something that a mass of people WANT to use, not just because it is part of their job and mandated by someone who probably has never even used said application. I enjoy getting feedback from customers and making my products better based on the overall needs of the user base.

That all being said we had something very exciting happen to us. We got mentioned on Web Worker Daily. This is huge as our app is really geared for that web based collaboration that a lot of web workers are geared for.

Hook another one for ColdFusion!

Nov
07
Filed Under (Personal) by J.J. Merrick

My brothers were featured on the local news in their town...