Railo Rewrite?

February 28, 2008 · By Gert Franz · 3 Comments

Nowadays when it comes to search engine compatibility a certain url of an application should be in a form that does not contain any url parameters. But sometimes an application or a framework demand certain URL forms. So what to do.There are basically two ways of doing so.
  1. Use SES format
    like http://www.web.com/index.cfm/action/overview/userID/15.
    The cgi.scriptname then contains a variable named path_info that contains the string (in this case) /action/overview/userID/15.
  2. use mod_rewrite for Apache
    This allows you full flexibility over handling certain URL's like:
    http://www.web.com/username
The first way is only possible if the application server allows SES formatted script names. And the second one, next to working only with Apache, needs you to handle the configuration and programming of the mod_rewrite module. So what to do if you work with IIS and for instance Jetty?

Well, use Railo for it. Unlike in other engines, the Application.cfc is called even for non existing .cfm files. So for instance, if you call a non existing page like: www.mycommunity.com/members/Overview/Tanja.cfm the application.cfc which is located somewhere inside or above the folder members gets involved. Let's assume normally you would call a fusebox action and pass the username in order to get the page for the user with the username Tanja. This URL might look like follows: www.mycommunity.com/members/index.cfm?fusebox.action=Overview&userID=34
Here's a way of how to implement that in the Application.cfc: <cfcomponent hint="Main entry point">
<cffunction name="onRequest" output="Yes" returntype="void">
<cfargument name="requestURL" required="Yes">
<cfset var aParams = ListToArray(arguments.requestURL, "/")>
<cfset var iParamLen = ArrayLen(aParams)>
<cfset var sUserName = ListFirst(aParams[iParamLen])>
<cfset var sAction = aParams[iParamLen -1]>
<cfquery name="getUserInfo" datasource="someDatasource">
SELECT   UserID
FROM   Users
WHERE   userName like '#sUserName#%'
</cfquery>
<cfif getUserInfo.recordCount neq 0>
<cfset url.userID = getUSerInfo.userID>
<cfset url["fusebox.action"] = sAction>
<cfinclude template="index.cfm">
<cfelse>
<cfinclude template="userNotExisting.cfm">
</cfif>
</cffunction>
</cfcomponent>

Tags: CFML · Features · Railo 2.0

3 responses so far ↓

  • 1 Jason // Feb 28, 2008 at 7:47 PM

    So could you call www.mycommunity.com/MyUserName and it still hit the app.cfc that is in the root dir? Then of course you could pull out the MyUsername part and query the db and proceed from there.

    Or does it have to end in .cfm?
  • 2 Gert Franz // Feb 28, 2008 at 8:49 PM

    Jason,

    you could configure your webserver to act like that. The only thing what you have to do is to configure the webserver to invoke the appserver defined for Railo for every directory lookup. And then let the appserver like resin handle any filetype with the Railo servlet.

    Gert
  • 3 Justin Carter // Feb 29, 2008 at 6:21 AM

    That's pretty cool that Railo supports this :) Good tip!
    For those on IIS, URL rewriting is still possible with this free ISAPI filter, I use it with all my FarCry sites:
    http://www.codeplex.com/IIRF/

Leave a Comment

Leave this field empty: