Dynamic Google Site Map in Coldfusion

Google Site maps are a valuable tool for any website owner, these files create a map for the search engine spider to efficiently navigate your website and update their index with your newest content. This will in turn help improve your overall search performance and ranking. This technology is especially important for dynamic driven websites with consistently updating content such as e-Commerce websites or blogs. Below is an example of some coldfusion code that generates a site map from a simple database structure. I think this will give you all a basic idea of what to do for your websites.


<cfprocessingdirective suppresswhitespace="yes">

<!--- set defaults --->
<cfparam name="category" default="0">
<cfparam name="url.auto" default="0">
<cfparam name="url.articlesupdated" default="">

<!--- set XML variables for sitemap --->
<cfset var.contentFolder = "sitemap.xml">
<cfset xmlHeader = '<?xml version="1.0" encoding="UTF-8"?>' & chr(13) & '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">'>
<cfset xmlFooter = "</urlset>">
<cfset xmlTagsStart = "<url>" & chr(13) & "<loc>">
<cfset xmlTagsEnd = "</loc>" & chr(13) & "<priority>0.5</priority>" & chr(13) & "<changefreq>daily</changefreq>" & chr(13) & "</url>" & chr(13)>

<!--- write blank file to clear data --->
<cflock scope="Session" type="ReadOnly" timeout="30" throwontimeout="no">
<cffile
action="write"
file="#ExpandPath(var.contentFolder)#"
output=""
>
</cflock>

<!--- set XML header text --->
<cflock scope="Session" type="ReadOnly" timeout="30" throwontimeout="no">
<cffile
action="write"
file="#ExpandPath(var.contentFolder)#"
output="#xmlHeader#"
>
</cflock>

<!--- get all active news and save output as variable --->
<cfsavecontent variable="dataOutputText">

<cfquery name="rsProductsSearch" datasource="#APPLICATION.dsn#" username="#APPLICATION.un#" password="APPLICATION.pw">
SELECT
* FROM news
</cfquery>

<cfquery name="catSearch" datasource="#APPLICATION.dsn#" username="#APPLICATION.un#" password="APPLICATION.pw">
SELECT * FROM categories WHERE category_archive <> 1
</cfquery>

<cfoutput>
#xmlTagsStart#http://www.newssite.com/index.cfm#xmlTagsEnd#
<cfloop query="rsProductsSearch">
#xmlTagsStart#http://www.newssite.com/?story=#rsProductsSearch.id##xmlTagsEnd#
</cfloop>
<cfloop query="catSearch">
#xmlTagsStart#http://www.newssite.com/?category=#id##xmlTagsEnd#
</cfloop>
</cfoutput>

</cfsavecontent>


<!--- output code for each category --->
<cflock scope="Session" type="ReadOnly" timeout="30" throwontimeout="no">
<cffile
action="append"
file="#ExpandPath(var.contentFolder)#"
output="#dataOutputText#"
>
</cflock>

<!--- Add the XML footer Code --->
<cflock scope="Session" type="ReadOnly" timeout="30" throwontimeout="no">
<cffile
action="append"
file="#ExpandPath(var.contentFolder)#"
output="#xmlFooter#"
>
</cflock>

<!--- Create/update Stats File 'googleStats.cfm' --->
<cflock scope="Session" type="ReadOnly" timeout="30" throwontimeout="no">
<cfset updateDateTime = "#DateFormat(Now(), "MM/DD/YY")# | #TimeFormat(Now(), "HH:mm:ss")#">
<cfset var.contentFolder = "googleStats.cfm">
<cffile
action="write"
file="#ExpandPath(var.contentFolder)#"
output="#updateDateTime#">
</cflock>

<!--- Terminate Script for scheduled tasks --->
<cfabort>
</cfprocessingdirective>

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Arizona Website Development by: OSEKmedia, llc
Blog Powered by: BlogCFC, version 5.9.002