Creating an accurate sitemap is a necessity for seo. In addition it serves as an important usability feature for your website. However building a sitemap manually can be a tedious job, and if you have a dynamic site (such as a blog) it can be even harder to keep your sitemap accurate.
Fortunately by using url rewriting and coldfusion we can create a dynamic site map which is built directly by our database on loading. Further we can apply this technique to styling the sitemap using xsl. Here's how.
Before creating a sitemap dynamically let first create one manually to detail the structure.
A standard xml file consists of the follow components.
As you can see it really is not that complicated to create a sitemap. Here is a simple sitemap listing the home page and a blog page.
http://www.neiland.net/
2011-03-05
daily
1
http://www.neiland.net/blog/
2011-03-05
weekly
0.8
In order to build a dynamic sitemap that replicates the above xml file we simply rename sitemap.xml to sitemap.cfm and add the follow two processing directives to the head of the file. These tell coldfusion to serve the file using the utf-8 encoding standard and to only output code that is wrapped in the cfoutput tags. We finally wrap our previous code with cfoutput tags so that the contents of the file are displayed.
Note: It is very important that you leave no spaces between the opening cfoutput tag and the xml declaration as any whitespace before the xml declaration invalidates the file.
http://www.neiland.net/
2011-03-05
daily
1
http://www.neiland.net/blog/
2011-03-05
weekly
0.8
We test that this is working by visiting "http://oursite/sitemap.cfm/" .If we get back an xml document then we are good to continue on to the next step.
Now that the sitemap is being served by coldfusion we can add to it dynamically. Here I will demonstrate how I add some blog entries to the listing.
select
url, dateupdated
from blog
limit 5
http://www.neiland.net/
2011-03-05
daily
1
http://www.neiland.net/blog/
2011-03-05
weekly
0.8
http://www.neiland.net/blog/#demo.url#/
#dateFormat(demo.dateupdated,"yyyy-mm-dd")#
never
0.5
As you can see its a simple matter of getting some data and outputting it. Note the use of dateformat to output a valid date for the lastmod child node. Finally there is one last step we can do.
At this stage if you go to http://yourdomain/sitemap.cfm you should see an xml file listing your url nodes. You could submit this to search engines as is but I prefer to hide that the file is dynamic by using this apache rewrite rule. (IIS has a similar url rewrite capability)
#Sitemap
RewriteRule ^sitemap.xml/?$ sitemap.cfm [L,NC]
So thats all there is to it. Now whenever googlebot crawls your site it will find an accurate up to date sitemap.xml file.