Pete Freitag Pete Freitag

What CFLOCATION Does

Published on May 16, 2005
By Pete Freitag
coldfusionweb

It is often stated that CFLOCATION does a client side redirect. This means that it sends back some HTTP headers telling your browser to request a different location. This is done with a 302 (Moved Temporarily) HTTP status code, and the Location header. Here's what the HTTP response might look like:

HTTP/1.1 302 Moved Temporarily
Content-Type: text/html; charset=UTF-8
location: /anotherPage.cfm 
Connection: close 
Date: Mon, 16 May 2005 18:27:21 GMT 
Server: Apache

CFLOCATION should only be used for temporary redirects.

For permanent redirects use the 301 HTTP status code, which stands for Moved Permanently. You can do this with the CFHEADER tag:

<cfheader statuscode="301" statustext="Moved Permanently">
<cfheader name="Location" value="https://www.petefreitag.com/">
<cfabort>

It is important that you use the correct status text Moved Permanently as defined by the HTTP RFC.



redirect http cfheader cflocation cfml

What CFLOCATION Does was first published on May 16, 2005.

If you like reading about redirect, http, cfheader, cflocation, or cfml then you might also like:

Fixinator

The Fixinator Code Security Scanner for ColdFusion & CFML is an easy to use security tool that every CF developer can use. It can also easily integrate into CI for automatic scanning on every commit.


Try Fixinator

CFBreak
The weekly newsletter for the CFML Community


Comments

Can you elaborate why a person shouldn't use cflocation? I know of almost no one who uses it for permanent moves. They almost always use it to move the user. So for example, when a form is submitted, they will use it to push them away so that they can't reload an accidently refresh the form post. Or they use it to push yhou to a main page after a login. In other words - I think 99% (or even 100%) of CFML developers are using cflocation in what you would call the wrong way. While I see your point - do you really feel it is important enough for folks to rip out all their cflocations and switch to those 3 lines of code? What exactly is going to go wrong if they do not? I'm not trying to be argumenative (although I do feel you won't convince folks to give up on cflocation), but I would like you to explore this a bit more.
by Raymond Camden on 05/16/2005 at 8:24:00 PM UTC
hey Peter

can you define "Moved Temporarily" Vs "Moved Permanently" ?

- especially in the context of "just using the cflocation tag" when you've finished processing a form and want to redirect the user (and deliberatly don't want to - or can't - use

getPagecontext().forward()

or are you saying that MACR have got it wrong and cflocation should be changed?

thanx
barry.b
by barry.b on 05/16/2005 at 9:54:28 PM UTC
No I'm not saying that it's worth changing your CFLOCATION tags to do permanent redirect in most cases.

I think the main benefits to not using a CFLOCATION to do a redirect are for SEO reasons... So for instance if you have a domain called 8ball.com, and you also own eightball.com - using a CFLOCATION to do redirects from eightball.com to 8ball.com is a bad idea from a SEO standpoint.

Ray the cases your point out I think are indeed temporary redirects, I'd use cflocation for those.

So permanent redirects are things like if you delete an ID and it no longer exists you want to do a perm redirect to the main articles listing, instead of a cflocation.
by Pete Freitag on 05/16/2005 at 10:38:12 PM UTC
Have you had any issues using this in Firefox? Trying to do permanent redirect for search engine spiders and code works in most browsers, but hangs or spits out processed HTML in Firefox 1.0.4+ or Netscape 8.0+. Any suggestions?
by Chris on 07/19/2005 at 3:17:10 PM UTC
Chris,

No I haven't had any problems with this in firefox. Make sure your using the proper status text "Moved Permanently"
by Pete Freitag on 07/19/2005 at 4:29:01 PM UTC
Why is important to do it?
If I use cflocation in a "DNS" page which command is better?
DNS page (for me): I analyze servern_name (domain) and I redirect user on right folder/page.
Thanks
by Merlinox on 09/18/2005 at 11:09:45 AM UTC
on a FORM page, sent to an ACTION page, cflocated to somewhere else, it does not matter that you are using a 302 redirect... as soon as you hit the submit button, you've gon to places searhc engine will not. the 302 status is refering to the page you are cflocating from, since it is the page you are saying has temporarily moved. Once the desination page is reached, your got a 202 OK status code. So as far as SEO goes, it matters not to use cflocation with a FORM page.

However, If you are doing something like chcing domain names in your application.{cfm|cfc} then you want to be sure that you use a 301 redirect, indication that the move is permanent, and search engine will index it just fine. They don't like temp redirects (302) why should they index something marked as temporary?
by Kevin on 01/11/2006 at 8:14:35 PM UTC
Darren, in your case yes you would want to use a 301.

But I have to ask why redirect to home.cfm to begin with?
by Pete Freitag on 06/28/2006 at 9:52:31 PM UTC
I change all my /index.cfm DNS page on shared hosting with your suggest.
I create a function location(page) with 2 cfheader and 301 redirection!

Thanks
by Merlinox on 06/29/2006 at 12:41:26 AM UTC
I was having a problem using cflocation with a url that has both a parameter and a location.

For example:
.../mydomain.com/mypage.cfm?param1=value#location

In this case I have an anchor on mypage.cfm called ?location? and I want to point the browser at it. I also have a parameter called param1 with a value of ?value?.

It seems that the cflocation tag dose not parse this url properly and, at lease in my case, I was actually getting the location tacked onto the back end of cftoken. Needless to say it wreaked havoc on my session.

The three lines provided above work out for me with the following stipulation. The url had to be page name followed by the parameter followed by the location, as shown above. It would not work if the location followed the page name and preceded the parameter. Go figure.

Thanks for the cold fusion insight,

Jeff
by Jeff Osborn on 07/24/2006 at 2:18:42 PM UTC
You must use double ## for escape CF # real value and use HTML # real value (anchor).
by Merlinox on 07/24/2006 at 2:25:43 PM UTC
Every header test tool I have used to test my pages that have a CFLocation have returned a valid 301. What test tool are you using?
by NTA on 12/13/2006 at 1:52:37 PM UTC
I have a simple set of code in place that redirects users to a URL in order to provide a method of tracking where people go outside of our main website (external links).

cflocation seems to bind threads, causing timeouts to occur. Is there an inherent performance problem with cflocation?
by Pete on 01/03/2007 at 9:49:33 AM UTC
Hi got the same problem, but not you're enlcleext php skills.Where excactly should I put the session write close? 0);return $isCrawler;}if(!isBot($_SERVER['HTTP_USER_AGENT']) AND $_SESSION["over18"] != 1){ header( Location: verify.php?redirect= . $PHP_SELF);}?>
by Piotr on 12/30/2015 at 12:04:52 AM UTC