Search Engine Friendly URL’s – coldfusion, cold fusion, CFML)
I have a client that has a site in coldfusion and I needed to fix their URL’s so they are search engine friendly. I don’t have access to the server so I needed a coldfusion solution. What I wanted was to make it so there were no ?, &, or = signs in the URL and convert them to /’s. I also wanted to 301 redirect the old URL’s to the new URL’s. Here is what I came up with
<cfset urlstring = cgi.path_info>
<cfloop from="1" to=#ListLen(urlstring,"/")# index="i">
<cfif i mod 2>
<cfset paramName = "URL." & ListGetAt(urlstring,i,"/")>
<cfelse>
<cfparam name="#paramName#" default="#ListGetAt(urlstring,i,"/")#">
</cfif>
</cfloop>
<cfif cgi.QUERY_STRING contains "=">
<cfset moveUrl = #rereplace (cgi.QUERY_STRING,"[=?&]","/","ALL")#>
<cfset newUrl = #cgi.path_info# & "/" & #moveUrl#>
<cfheader statuscode="301" statustext="Moved permanently">
<cfheader name="Location" value="#newUrl#">
</cfif>

I almost got goosebumps seeing ColdFusion code in a SEO blog post. How fun! Just a little jolt of adrenaline to an old CF’er turned SEO.
Is it still necessary to remove the “?” and other symbols from CF code so search engines can read them?
Thanks!
No it is not necessary. Google does a good job of treating those as pages. There are other reasons to avoid the question mark. I will make a post about it.
Nicely done!
I had looked into the path_info variable while writing an application but I wasn’t sure exactly what it did. I didn’t even think about the permanent redirect as well, but it makes sense to do.
[…] Hello
Great resource! I’ve been meaning to do a similar post. It’s interesting.
Alex Bell […]
Hi Great Script, It works wonderfully…
I do have a possible glitch however. When I try to submit a form from a page loaded through the sEFurls script, my cf code is stating that the #form.variable# isn’t defined.
However if I remove the sefurls script there is no issue. Any Idea?
@james
Is your form method value “post” or “get” and is your action page using the respective scope?
I’ve noticed something weird with this script on IE 6. I occasionally get an error report where the page fails because for some reason IE 6 is some how loading the favicon.ico as a parameter even though I deleted it.
PATH_INFO: /cID/favicon.ico
Any suggestions?
Thanks!
@derek:
The form uses the post method, and the SEFurls is included in my application.cfm file.
Hello,
I tried your script and it works except for my webpage template logo/images don’t show up. Any suggestions???
I”m not a pro CF programmer. I just do searches on google to get syntax. I’m good at getting small things done with programming languages but nothing very complex. You could post your problem on a CF forum.
@derek:
is your action page using the respective scope?
I don’t quite understand what you’re meaning by this…
Thanks for the post. When I dropped this into my application.cfc it killed my css. Any thoughts?
Guys,
This is a path issue. The script is modifying the base path.
A simple, but dirty, fix is to use absolute URL’s when referencing your images, css, scripts, etc.
This isn’t an ideal solution, but the SEF URL’s isn’t ideal either. If you are on your own windows box, try ISAPI rewrite.
Good luck.
Guys,
Sorry I might be asking the obvious here but am I supposed to add something else with this code for it to work? when I added this code to my application.cfm file it just gave me a flat 404 error.
I’m new to this so some guidance would be much appreciated.
Thanks.
It has been a while since I wrote this and have not worked with cold fusion since. I wish I had made documentation. Sorry. I don’t work with that client any more and don’t have access to a coldfusion server to tetst.
what happens when someone copies the new url, sends it to a friend, and then the friend clicks the link? wouldn’t you need to reverse your logic in that situation in order for the url to work?
I am interested in this too. I generally take the position that Google has “figured out” how to deal with the ‘?’. So I don’t *think* there is any SEO value either way any more. I think that once upon a time, the ? was considered dirty. But not really anymore.
BUT – I do think there is a user experience issue. And also, as mentioned above, there is the emailing issue. Also, I’ve seen issues submitting url’s with ? and & into certain bookmarking sites. It would still be nice to make it go away. Is this possible in CF?
Thx
Google seems to read “?” okay in URL, but to my understanding, page rank doesn’t flow too well from the main page to the sub-directory if there is ? in it?