ColdFusion Simple Friendly URLs
There quite a few methods out there for making search engine and user friendly URLs in ColdFusion eg Spike's Friendly URL servlet or one of the many Apache and IIS URL rewiters.
These are great if your hosting provider has installed them or if you have your own dedicated server. So what do you do if this is not the case or you want to support friendly URLs in a web server agnostic way?
Most ColdFusion developers know you can access the URL scope, but may not be aware that it can be altered as well. Once you know this it's an easy matter to turn URLs like this:
http://www.yourdomain.com/product.cfm/category/books/product/23
Into this:
http://www.yourdomain.com/product.cfm?category=books&product=23
All you need to do is look at everything past the '.cfm' in the URL, split into name value pairs and place into the URL scope.
And place this code in your Application.cfm file or onRequest handler of your Application.cfc file.
2 rhs = right(cgi.PATH_INFO,len(cgi.PATH_INFO)-len(cgi.SCRIPT_NAME));
3 if (rhs neq "") {
4 noitems = listLen(rhs,"/");
5 for (i=1; i lte noitems; i = i + 2) {
6 item = listgetat(rhs,i,"/");
7 url[item] = listgetat(rhs,i + 1,"/");
8 }
9 }
10</cfscript>
This will work for all URLs in the form:
http://www.yourdomain.com/page.cfm/var1/value1/var2/value2/var3/value3...
If you want to go one step further and support URLs in the form:
http://www.yourdomain.com/product.cfm/books/23
You can do this:
2 rhs = right(cgi.PATH_INFO,len(cgi.PATH_INFO)-len(cgi.SCRIPT_NAME));
3 if (rhs neq "") {
4 noitems = listLen(rhs,"/");
5 for (i=1; i lte noitems; i = i + 1) {
6 value = listgetat(rhs,i,"/");
7 switch (i) {
8 case 1:
9 url.category = value;
10 break;
11 case 2:
12 url.product = value;
13 break;
14 }
15 }
16 }
17</cfscript>
If different ColdFusion pages require different URL parameters you can just do a switch on the the script name and set different variables for each name.
Update: In order for forms and links (and other URLs) on the page to point to the correct location you need to either:
- Use absolute URLs on the page.
- Add a base tag to the header of the page.
You can automatically work out the base href like so:
There are two solutions I see:
a) Use absolute URLs on any page you want to access via friendly URLs
b) In Application.cfm (or .cfc) look for URL's containing two .cfm's, change the URL to what it should be and do a server side redirect to go to the correct URL.
I'll have a look into the second option to see how viable is it.
Something like this:
<base href="<cfoutput>http://#cgi.HTTP_HOST##ListDeleteAt(cgi.SCRIPT_NAME, ListLen(cgi.SCRIPT_NAME, '/'), '/')#/</cfoutput>" />
http://www.codeodor.com/index.cfm/2007/4/6/Did-you...
I am a newbee to Coldfusion. How doyou hide the blog id create with CreateUID() function?
You have something like http://blog.classsoftware.com/index.cfm/2007/3/30/... , how did you translate
/2007/3/30/Simple-Friendly-URLs back to blog id and get the details for Simple-Friendly-URLs?
Please help.
Thank you
You can download the code from http://blogcfc.riaforge.org/ to see exactly how he does it.
He uses a similar method to that described above I believe.
The 2 parameter of the Right function, which is now 0, must be a positive integer
The error occurred in C:\websites\53511bfb\properties\Application.cfm: line 2
1 : <cfscript>
2 : rhs = right(cgi.PATH_INFO,len(cgi.PATH_INFO)-len(cgi.SCRIPT_NAME));
3 : if (rhs neq "") {
4 : noitems = listLen(rhs,"/");