Dave asks:

I have a lot of city pages for my site similar to this folder structure: example.com/colorado/denver.cfm

What I have been doing is to create a template called template-city.cfm that gets included in to each city page such as denver.cfm. So basically denver.cfm is just a tiny shell file and the real guts of the page is located in the template-city.cfm, so the thought is that I only have to change the template-city.cfm once and all the others pages like denver.cfm get updated at once.

But in order to do this I had to create tons of city pages within each state (which I automated with CFfile) but a red flag is going off in my brain telling me this is the wrong way to do it. Is there some virtual way to create these city pages and even state folders?

There are two ways of doing this. The first would be with a server side URL rewriter. Apache has this built in, and you can find options for IIS as well. The URL rewriter would simply map example.com/louisiana/lafayette.cfm to example.com/dyncity.cfm?state=louisiana&city=lafayette.cfm.

If you don't have access to the web server and you are using ColdFusion 8, there is no reason not to use onMissingTemplate in Application.cfc. Here is a simple example:

<cffunction name="onMissingTemplate" access="public" returnType="boolean" output="true"> <cfargument name="pageRequested" type="string" required="true"> <cfset var city = listLast(arguments.pageRequested, "/")> <cfset var state = listGetAt(arguments.pageRequested, listLen(arguments.pageRequested,"/")-1, "/")> <cfset city = replaceNoCase(city, ".cfm", "")>

<cfinclude template="dyncity.cfm"> <cfreturn true>

</cffunction>

Given a request of some.com/louisiana/lafayette.cfm, the value passed to pageRequested will be a string I can parse using list functions. I grab the city at the end, and the state at the second to last position. At that point, what you do is up to your model. You would probably call a CFC method that would translate a state/city string to a particular ID value of a city in the database. In my example I just include a file that displays it:

<cfoutput><h2>#city#, #state#</h2></cfoutput>

I've included the application as a zip to this blog entry. You should be able to extract it to your web server, and then hit any URL. The only drawback is that .cfm must be in the URL.

If you wanted to support example.com/louisiana/lafayette, then you would need to use the URL rewriters I mentioned above.

Download attached file.