Cflocation for 301 redirects

I am renaming an existing file for the project I'm working on. To maintain backward compatibility, I leave the cfm file to redirect users to a new one.

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

To keep everything as clean as possible, I want to send a response status code of 301 if the user is trying to switch 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 cfheader tags.

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

Are there any reasons for using one method over another?

+8
redirect coldfusion cflocation
source share
2 answers

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

+11
source share

I tested this on ColdFusion 9.

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

From the Adobe ColdFusion Documentation :

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

So you will 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 run the risk of running into problems if other code runs after the cfheader tag. I came across this when I fixed code that added redirects to the application.cfm file - using cfheader - without interrupting the rest of the page processing.

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

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

You might want to add these headers if you need to use the cfheader tag with the position:

 <cfheader name="Cache-Control" value="no-cache"> <cfheader name="Pragma" value="no-cache"> 
+4
source share

All Articles