cflocation vs cfheader for 301 redirects

I think they do the same thing, with <cflocation> being more readable


I tested this on ColdFusion 9.

There is one major difference, and it is that cflocation stops execution of the page and then redirects to the specified resource.

From the Adobe ColdFusion documentation:

Stops execution of the current page and opens a ColdFusion page or HTML file.

So you would need to do this:

<cfheader statuscode="301" statustext="Moved permanently">
<cfheader name="Location" value="http://www.example.com/shop.cfm">
<cfabort>

to get the equivalent of this:

<cflocation url="shop.cfm" statuscode="301" addtoken="false">

Otherwise, you risk running into issues if other code runs after the cfheader tag. I came across this when fixing some code where redirects were inserted into an application.cfm file -- using cfheader -- without aborting the rest of the page processing.

I also noticed, in the response headers, that cflocation also sets the following headers accordingly:

Cache-Control: no-cache
Pragma: no-cache

One might want to add these headers in if using the cfheader tag with Location, if needed:

<cfheader name="Cache-Control" value="no-cache"> 
<cfheader name="Pragma" value="no-cache">