ColdFusioning: Pronunciation \kold-fy�-zhn-ing\ Noun: The action of one that writes ColdFusion.

Google and Yahoo Sitemaps

Sitemaps can be one of the more important, but often overlooked, things you can do to help search engines find all the pages in your site. While some people create them by hand or use a 3rd party sitemap generator, I prefer to use ColdFusion to generate them.

The code below has three variables that need to be set in the first three lines. You can use the code after "Other Page" to suit your specific situation especially if you need to list pages in a database.

I used this code to generate the sitemap for My Cool Backgrounds and you can view the sitemap here: http://www.mycoolbackgrounds.com/sitemap.xml. Be warned though, it has almost 10K urls and might take a minute to load.

After you have generated your sitemap, you can submit it to Google Webmaster Tools and Yahoo Site Explorer. They both will accept the same format for the sitemap.


<cfset request.addy = "http://www.example.com/" />
<cfset request.root = "/www/example.com/public/" />
<cfset PageList = "main/about,category,contact" />

<cfset iCount = 1 />

<cfset theXML = XmlNew() />
<cfset theXML.urlset = XmlElemNew(theXML,"urlset") />
<cfset theXML.urlset.XmlAttributes.xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" />

<!------------------------------------------------------------------------------------------
Main Page:
------------------------------------------------------------------------------------------->

<cfset theXML.urlset.XmlChildren[iCount] = XmlElemNew(theXML,"url") />

<cfset theXML.urlset.XmlChildren[iCount].loc = XmlElemNew(theXML,"loc") />
<cfset theXML.urlset.XmlChildren[iCount].loc.XmlText = "#request.addy#" />

<cfset theXML.urlset.XmlChildren[iCount].lastmod = XmlElemNew(theXML,"lastmod") />
<cfset theXML.urlset.XmlChildren[iCount].lastmod.XmlText = DateFormat(Now(), "YYYY-MM-DD") />

<cfset theXML.urlset.XmlChildren[iCount].changefreq = XmlElemNew(theXML,"changefreq") />
<cfset theXML.urlset.XmlChildren[iCount].changefreq.XmlText = "daily" />

<cfset theXML.urlset.XmlChildren[iCount].priority = XmlElemNew(theXML,"priority") />
<cfset theXML.urlset.XmlChildren[iCount].priority.XmlText = ".9" />

<cfset iCount = incrementValue(iCount) />

<!------------------------------------------------------------------------------------------
Other Pages:
------------------------------------------------------------------------------------------->

<cfloop index="txtItem" list="#PageList#">
<cfset theXML.urlset.XmlChildren[iCount] = XmlElemNew(theXML,"url") />

<cfset theXML.urlset.XmlChildren[iCount].loc = XmlElemNew(theXML,"loc") />
<cfset theXML.urlset.XmlChildren[iCount].loc.XmlText = "#request.addy##txtItem#/" />

<cfset theXML.urlset.XmlChildren[iCount].lastmod = XmlElemNew(theXML,"lastmod") />
<cfset theXML.urlset.XmlChildren[iCount].lastmod.XmlText = DateFormat(Now(), "YYYY-MM-DD") />

<cfset theXML.urlset.XmlChildren[iCount].changefreq = XmlElemNew(theXML,"changefreq") />
<cfset theXML.urlset.XmlChildren[iCount].changefreq.XmlText = "daily" />

<cfset theXML.urlset.XmlChildren[iCount].priority = XmlElemNew(theXML,"priority") />
<cfset theXML.urlset.XmlChildren[iCount].priority.XmlText = ".8" />

<cfset iCount = incrementValue(iCount) />
</cfloop>

<cffile action="write" file="#request.root#sitemap.xml" output="#lcase(Trim(theXML))#" />
<h3>SiteMap Generated</h3>



Comments