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...

Subscribe
Subscribe via RSS
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

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).
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.
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.
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?
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.
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
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.
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.