Google Sitemap support for BlogCFC

This week I added support for Google Sitemaps to my blog. While this isn't in the core BlogCFC install yet, I thought I'd share the code in case others wanted to do it. Right now I've just created a new CFM and dropped the code in there. I will most likely move the generation to a CFC method later on. Also note that I was unable to get the sitemap to verify until I "tricked" google. My file name was, let's say, sitemap.cfm. This refused to validate. I saved the output of my file as test.xml, and that validated immidiately. I hunted around and found a few people who asked if the extension would cause problems for Google. On a whim, I resubmitted by sitemap with this url: http://ray.camdenfamily.com/sitemap.cfm?ext=xml. (Not the real URL.) This was enough to "fool" Google and allow my sitemap to validate. The code is below.

Note - the code belows has MY url in it. You would obviously want to change that. The "real" code in 4.0 won't require that.

<cfsetting enablecfoutputonly=true showdebugoutput=false>
<cfprocessingdirective pageencoding="utf-8">

<cfset params = structNew()>
<!--- Should be good for a while.... --->
<cfset params.maxEntries = 99999>
<cfset params.mode = "short">

<cfset entries = application.blog.getEntries(params)>

<cfset z = getTimeZoneInfo()>
<cfif not find("-", z.utcHourOffset)>
   <cfset utcPrefix = "-">
<cfelse>
   <cfset z.utcHourOffset = right(z.utcHourOffset, len(z.utcHourOffset) -1 )>
   <cfset utcPrefix = "+">
</cfif>

<cfset dateStr = dateFormat(entries.posted[1],"yyyy-mm-dd")>
<cfset dateStr = dateStr & "T" & timeFormat(entries.posted[1],"HH:mm:ss") & utcPrefix & numberFormat(z.utcHourOffset,"00") & ":00">

<cfcontent type="text/xml"><cfoutput><?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
   <url>
      <loc>http://ray.camdenfamily.com/</loc>
   <lastmod>#dateStr#</lastmod>
      <changefreq>hourly</changefreq>
      <priority>0.8</priority>
   </url>
   </cfoutput>
   <cfoutput query="entries">
      <cfset dateStr = dateFormat(posted,"yyyy-mm-dd")>
      <cfset dateStr = dateStr & "T" & timeFormat(posted,"HH:mm:ss") & utcPrefix & numberFormat(z.utcHourOffset,"00") & ":00">
      <url>
      <loc>#xmlFormat(application.blog.makeLink(id))#</loc>
      <lastmod>#dateStr#</lastmod>
      </url>
   </cfoutput>
<cfoutput>
</urlset>
</cfoutput>

Archived Comments

Comment 1 by Luis Majano posted on 9/16/2005 at 6:59 AM

Hey Ray,

I just posted it on my blog. http://www.luismajano.com/b...

Thanks!!

However, I forgot to submit with the sitemap.cfm?ext=xml, just sitemap.cfm and it validated with no problem.

Just to let you know.

Comment 2 by Merlinox posted on 1/26/2006 at 10:26 PM

Are a CFC or CFX able to create a sitemap (or other data structure), analyzing site from a root page?

Comment 3 by Raymond Camden posted on 1/26/2006 at 10:38 PM

I'm not aware of any. I mean sure you can use Verity to index your site, but it won't return something you can build a site map from. I really tihnk you will need to do it manually.

Comment 4 by Merlinox posted on 1/27/2006 at 2:08 AM

Also CrystalTech support has replied me like you. But my site is all db based...
Like I wrote on CT forum I'm creating an index system based on xml sitemap. But maybe a good thing if sitemap may generate automatically by a scheduled task.

Comment 5 by JOSH posted on 9/1/2006 at 11:19 PM

I created some stuff similar to this for CF Developers on <a href="http://googlebotsnacks.com">GoogleBotSnacks.com</a>

Comment 6 by Mark posted on 12/10/2008 at 2:07 PM

Thanks for the tip on how to get Google Sitemaps to deal with having a Coldfusion page as the sitemap. Much appreciated.