Coldfusion Mod Rewrite

I read an article by Sam Farmer the other day where he talked about a little project called Short URL. I thought the use of the onMissingTemplate method in the Application.cfc to catch the non-existing template and do a lookup for the longer URL to be quite interesting. But I want search engine safe (SES) urls that also carry a meaning.

For example the URL: http://www.garyrgilbert.com/tutorials/coldfusion/advanced/rss.cfm Here I have a page named rss in the category of coldfusion and subcategory of advanced. A real URL that goes to a real page in the real directory structure. But we could easily fake this URL by using the onMissingTemplate method as well. In essence we could create our own mod rewrite.

If the above URL was fake it would trigger the onMissingTemplate method in the Application.cfc. We could then take the URL and parse it (or send it to a component to do) according to rewrite rules we specify:

Category = coldfusion Sub-category= advanced page = rss

We would then display the results.

This works fine if the URL we have ends with .cfm as above, the tricky bit comes when we want to view all advanced tutorials as in www.garyrgilbert.com/tutorials/coldfusion/advanced/.

With this URL coldfusion doesn't even get to look at this URL as it is referencing a directory that doesn't exist you end up getting a 404 not found. Not so good.

To solve this we could create our own 404 page with coldfusion and internally manipulate the URL, for example add index.cfm to the end of all URL's without a file reference, and then do a cflocation. Your onMissingTemplate method would then need to handle this according to your re-write rules.

I tested it and it functions in practice, I just don't know how efficient or scalable using the onMissingTemplate method as the main "controller" would be. It will need to be stress tested before being used in a high traffic site.

Happy Coding...

12 Comments to "Coldfusion Mod Rewrite"- Add Yours
orangepips's Gravatar Aside from they way you've described, or doing it at the web server (BTW there are several IIS equivalents of mod_rewrite out there), I've also seen it done with at the J2EE server level using a rewrite filter, which uses syntax very similar to the Apache's (i.e. regular expressions):

http://tuckey.org/urlrewrite/

Note, this presupposes your are running Coldfusion in a J2EE or EAR/WAR configuration.

That said, as you wrote, my fear is always how well will it respond under load. And, I work on something where I cannot rely on the extension to determine what handles the file. So I've opted for a web server based solution (mod_rewrite on Apache and an ISAPI filter on IIS).
# Posted By orangepips | 1/2/08 7:06 PM
James's Gravatar Hello Gary,

Very cool article!

I have been trying to implement the SEO SWFAddress example into a flash site I am working on. This is the SEO example:

http://www.asual.com/swfaddress/samples/seo/

I am unable to perform the URL rewriting with the .htaccess file because my website is hosted at CrystalTech on a ColdFusion shared Windows server where they don't have something like ISAPI. The server does have PHP 5. Do you think your technique will integrate into SWFAddress without causing any cloaking issues with search engines?

Thank you.
# Posted By James | 2/19/08 1:38 AM
Gary's Gravatar @James,

you will probably have to create a special 404 page that decodes your url and the do a cfinclude or however you process you page.
You may also want to consider setting the statuscode to 202 (<cfheader statuscode="202">) so that it's not recorded as a 404 not found.
# Posted By Gary | 2/20/08 11:50 PM
Radek's Gravatar How did u make this URL?
http://www.garyrgilbert.com/blog/index.cfm/2008/1/...

I know it works for php .htaccess but I was trying to find coldfusion mod rewrite and did not find anything except your page and I can see in the link you did it. How?
# Posted By Radek | 2/23/08 3:04 AM
Gary's Gravatar @Radek,

The blog url is search engine safe but I didn't write it, it's part of blogCFC. It also isnt that hard to do. My article
was more looking at other ways to do SES URL's. If the way the URL is done on this blog suits your needs
meaning http://<yoursite>/index.cfm/<param>/&l... then you could just download Blog CFC and look at
the parseSES.cfm page to see how it is done, and modify it to suit your needs.
# Posted By Gary | 2/24/08 4:48 PM
Radek's Gravatar Thanks I am gonna check that out. BAsically what I want trying to do is this:

http://www.examplesite.com/word.cfm?productoid=36&...

to change to this (SEO friendly)

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

is that possible in coldfusion? Thanks
# Posted By Radek | 2/24/08 10:35 PM
Gary's Gravatar Yes it's possible with Coldfusion

Without too much work you could create your URL to do something like

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

if you want it without the index.cfm you need to do a bit more work as I described in my post.
# Posted By Gary | 2/25/08 12:18 PM
Radek's Gravatar What I want to do is turn online store with all pages like I showed you in previous post to pages like Books/Science of coures can be like index.cfm/Books/Science....so I can use BlogCFC for that? Or I have to do it other way?
# Posted By Radek | 2/25/08 1:00 AM
Sergio's Gravatar I ran across this article searching for a way to spoof directories similar to MySpace & Picasa Web Albums. (www.myspace.com/User and www.picasaweb.google.com/User) I am sure they do not have millions of user folders in thier web root, so how are they doing this? I have been researching this for days and have not found a thing. I am on a CF7 server and cannot use the examples provided here. Any help in the right direction would be greatly appreciated!
# Posted By Sergio | 5/29/08 9:03 PM
Gary Gilbert's Gravatar Sergio,

If you are on a hosted environment which uses IIS and you dont have the option of an ISAPI filter the only way that I know to do it is using the 404 handler. All ISP's should allow you to provide a custom 404 file handler you could create one using coldfusion (test it on your local machine first).

When a directory or file is not found IIS routes the call including the asked for URL to the 404 page, you could take that and parse the URL. For example:

http://mysite.com/parishilton

you strip off your webroot which leaves you with "parishilton" do a lookup in your users table and if parishilton exists then display their page. If you don't find a user with the requested name you can then display your own custom NOT FOUND message.

You also have to think about providing your users with their own virtual subdirectories too like

http://mysite.com/parishilton/home" target="_blank">http://mysite.com/parishilton/home
http://mysite.com/parishilton/aboutMe" target="_blank">http://mysite.com/parishilton/aboutMe
http://mysite.com/parishilton/blog" target="_blank">http://mysite.com/parishilton/blog
http://mysite.com/parishilton/blog" target="_blank">http://mysite.com/parishilton/blog/05/30/2008/I_hate_my_life.cfm" target="_blank">http://mysite.com/parishilton/blog" target="_blank">http://mysite.com/parishilton/blog/05/30/2008/I_ha...

This means of course you need to establish a URL alias pattern. To make this work you don't have to have CF 8 you just have to be a bit more inventive.
# Posted By Gary Gilbert | 5/30/08 12:34 PM
# Posted By asdf | 8/16/08 9:32 AM
# Posted By Scott | 8/18/08 8:39 PM

Powered By Railo

Subscribe

Subscribe via RSS
Follow garyrgilbert on Twitter Follow me on Twitter
Or, Receive daily updates via email.

Tags

adobe air ajax apple cf community cfml coldfusion examples ext flash flex google javascript max2007 max2008 misc open source programming railo software technology ui

Recent Entries

Converting structkeys to lowercase

Blogroll

An Architect's View
CFSilence
Rey Bango
TalkingTree

Wish List

My Amazon.com Wish List