Submit Your Site For Free!

Email Address:
* URL:
*
*Indicates Mandatory Field

Terms & Conditions

DevWebProCA
FlashNewz
DevWebPro








Automatically Pushing To A New URL Made Simpler

By Raymond Camden
Expert Author
Article Date: 2007-06-11

When I was preparing to move my site from http://ray.camdenfamily.com to http://www.coldfusionjedi.com, I certainly didn't want all of my old URLs to stop working.

I wrote some quick code (literally, I forgot to write the code before hand so I wrote it live on the old server) to handle this.

Here is what I did...

First off - I read Pete Freitag's excellent blog post, What CFLOCATION Does, where he talks about how the status code sent out by CFLOCATION is intended for temporary move, not a permanent one. Using the code he suggested, I added a cfheader to my site. However, I have two styles of URLs. I have both traditional query_string style:

http://ray.camdenfamily.com/index.cfm?mode=entry&entry=XXXX

And my SEO style cgi.path_info style:

http://ray.camdenfamily.com/index.cfm/noone/reads/code/samples

So I needed code to notice either of these two forms. This is what I came up with:

<cfset loc = cgi.script_name>
<cfif len(cgi.query_string)>
  <cfset loc = loc & "?" & cgi.query_string>
<cfelseif len(cgi.path_info)>
&nbps; <cfset loc = loc & cgi.path_info>
</cfif>
<cfset loc = replaceNoCase(loc,"index.cfm/index.cfm","index.cfm")>

<cfheader statuscode="301" statustext="Moved Permanently">
<cfheader name="Location" value="http://www.coldfusionjedi.com#loc#">
<cfabort>


As you can see - I check both cgi.query_string and cgi.path_info.

Depending on what is used, I build up my new URL in a variable called loc. I can then push the user there using the two cfheader tags.

Now how does ColdFusion 8 make this easier? The cflocation tag has been updated to allow you to pass in a status code.

There is a set of valid status codes, of which 301 is one of them.

The two tags above would become one. (Actually I'd be able to get rid of the cfabort as well.)

Comments

About the Author:
Raymond Camden, ray@camdenfamily.com
http://ray.camdenfamily.com

Raymond Camden is Vice President of Technology for roundpeg, Inc. A long time ColdFusion user, Raymond has worked on numerous ColdFusion books and is the creator of many of the most popular ColdFusion community web sites. He is an Adobe Community Expert, user group manager, and the proud father of three little bundles of joy.




Newsletter Archive | Article Archive | Submit Article | Advertising Information | About Us | Contact

DevWebProCA is an iEntry, Inc.® publication - 1998-2008 All Rights Reserved Privacy Policy and Legal