Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cflocation vs cfheader for 301 redirects

I am "renaming" an existing file for a project I am working on. To maintain backwards compatibility, I am leaving a cfm file in place to redirect the users to the new one.

  • buy.cfm: old
  • shop.cfm: new

In order to keep everything as clean as possible, I want to send the 301 statuscode response if a user tries to go to buy.cfm.

I know that I can use either cflocation with the statuscode attribute

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

or I can use the cfheader tags.

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

Are there any reasons to use one method over the other?

like image 368
Joe C Avatar asked Jan 23 '13 05:01

Joe C


2 Answers

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

like image 192
Henry Avatar answered Oct 15 '22 18:10

Henry


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">
like image 38
Andy Tyrone Avatar answered Oct 15 '22 17:10

Andy Tyrone