Turn <cflocation> redirects ON and OFF?

Viewed 121

I have a legacy ColdFusion system with over 200 <cflocation> tags, and would like to be able to easily turn them all off for testing purposes from within my Application.cfm.

What would be the best way to do this other than having to add a conditional statement to every file?

The thought of extending the <cflocation> tag to first check a true/false flag came to mind, but I have never done anything that advanced before, and have no idea where to start.

1 Answers

You cannot extend/inherit native tags in CF. You could go with a custom tag and then search-replace all occurences of <cflocation (native) with <cf_location (custom) having your redirect flag inside of it.

Application.cfm

<cfset APPLICATION.allowRedirects = false>

location.cfm (wrapper for native cflocation)

<!--- if the variable doesn't exist for some reason, process the regular cflocation --->
<cfif (
    (not isDefined("APPLICATION.allowRedirects")) or
    (isBoolean(APPLICATION.allowRedirects) and APPLICATION.allowRedirects)
)>
    <cflocation attributeCollection="#ATTRIBUTES#">
    <!--- cflocation ends the request automatically --->
</cfif>

<!--- print debug info --->
<cfif structKeyExists(ATTRIBUTES, "url")>
    <cfoutput>Attempted to redirect to: #encodeForHtml(ATTRIBUTES["url"])#</cfoutput>
</cfif>

<!--- abort the request without redirecting --->
<cfabort>

Another approach would be <cfmodule>. Search-replace all occurences of <cflocation with <cfmodule template="/mytags/location.cfm". Advantage here: You don't need to register location.cfm via customTagPaths since cfmodule works via template path.


Even simpler: Just search-replace all of your <cflocation> tags in one go and wrap them in a conditional. You won't have to go through your 200 files manually.

(This is for Sublime Text, but any proper editor can do this for you.)
Find: (<cflocation[^>]+>) (enable RegEx!)
Replace: <cfif APPLICATION.allowRedirects>\1</cfif>
(\1 is the backreference, some editors use $1 instead)

Related