Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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">

To elaborate on the Answer by Andy Tyrone, while they MAY do the same thing in certain circumstances, the CFHEADER method give you more control over the headers passed in the request. This becomes useful, for example, if you want to send cache control headers to a browser or content delivery network so that they do not keep hitting your server with the same old redirect request. There is no way (to my knowledge) to tell a CFLocation to cache the redirect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top