Generating a sitemap is pretty easy but I figured someone might find a more detailed how-to useful. Earlier today I was taking a look at our sitemap and realize that I needed to make a few changes. Often times with something like this you might be tempted to turn to an add-on in order to generate the site map. I did a quick search and turned up a few solutions on Devot-ee. But I’ve created these before so I decided to just create one.

Creating a dynamic sitemap is not hard, and they certainly help your Search Engine Rank. The basic use of a sitemap is to tell Google when a page has been added, how often it gets updates, and when it was last updated so they can keep their information about your site current.

I’m in a good mood as the sky is blue, the birds are singing, and my youngest son has a baseball game this evening. So, before you decide to purchase yet another add-on let’s take a look at what you can do natively with ExpressionEngine.

The first step is to create the template as an XML document. If you are not aware of how to do this then you might want to give us a call.

Once you’ve created yours you’ll want to follow the format as follows. The opening declaration to tell what the file is:

<?xml version="1.0" encoding="UTF-8"?>
<urlset
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.sitemaps.org/schema...
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd”>

Then the format for each of the entries:


<url>
  <loc>http://bluefishds.com/</loc>
  <lastmod>2014-04-02T19:18:31+00:00</lastmod>
  <changefreq>monthly</changefreq>
</url>

This is where you can use ExpressionEngine to make it update automagically. I’ll admit that some of the pages like our About Us page or our Contact Page I just leave in as a static entry. But for something like our blog I use the following code:

{exp:channel:entries channel="blog|work|staff"}
<url>
  <loc>{comment_url_title_auto_path}</loc>
  <lastmod>{edit_date format="%Y-%m-%dT06:00:00+00:00"}</lastmod>
  <changefreq>weekly</changefreq>
</url>
{/exp:channel:entries}

Obviously, you’ll want to change the URL structure to whatever your site is. And so long as you change the Channel specified in the Channel Entries Tag Pair you can use this same snippet of code for your staff pages, work pages, and whatever else might get a regular entry added to it on your site.

Just make sure to close the urlset tag with:

</urlset>

So a completed Sitemap XML file for ExpressionEngine might look something like this…

<?xml version="1.0" encoding="UTF-8"?>
<urlset
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.sitemaps.org/schema...
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
  <loc>http://bluefishds.com/</loc>
  <lastmod>2014-04-02T19:18:31+00:00</lastmod>
  <changefreq>monthly</changefreq>
</url>
<url>
  <loc><loc>http://bluefishds.com/about-us</loc>
  <lastmod>2014-04-02T19:18:31+00:00</lastmod>
  <changefreq>monthly</changefreq>
</url>
<url>
  <loc><loc>http://bluefishds.com/work</loc>
  <lastmod>2014-04-02T19:18:31+00:00</lastmod>
  <changefreq>monthly</changefreq>
</url>
<url>
  <loc></loc><loc>http://bluefishds.com/blog</loc>
  <lastmod>2014-04-02T19:18:31+00:00</lastmod>
  <changefreq>monthly</changefreq>
</url>
<url>
  <loc><loc>http://bluefishds.com/contact-us</loc>
  <lastmod>2014-04-02T19:18:31+00:00</lastmod>
  <changefreq>monthly</changefreq>
</url>
 
{exp:channel:entries channel="blog|work|staff"}
<url>
  <loc>{comment_url_title_auto_path}</loc>
  <lastmod>{edit_date format="%Y-%m-%dT06:00:00+00:00"}</lastmod>
  <changefreq>weekly</changefreq>
</url>
{/exp:channel:entries}
</urlset>


Hope that is helpful. Much better than paying for an add-on. 

ExpressionEngine Blog E-Book

Over the last 10 years we have created lots of websites using ExpressionEngine. This short tutorial e-book will teach you some basics, but it will also show you some of the little details that you can use to really harness the power of EE. Get your copy now!