Create a Simple Sitemap Using Perch CMS

This post is old. It may contain broken links, missing images and incorrect information.

I’ve been using Perch for over a year now on all kinds of client and personal sites, and it is amazing!

Recently I’ve been looking at improving the performance of sites built with Perch – both from a speed perspective and also in terms of SEO (Search Engine Optimisation).

Here’s how I go about creating a simple sitemap for a site built using Perch:

Create a New Navigation Template

Within the Perch navigation templates folder I create a new file called sitemap.html:

/perch/resources/templates/navigation/sitemap.html

This file contains the following code:

<perch:before><?xml version="1.0" encoding="UTF-8"?>
<urlset >
</perch:before>
<url>
    <loc>https://YOURDOMAIN.co.uk<perch:pages id="pagePath" /></loc>
</url>
<perch:after>
</urlset>
</perch:after>

Don’t forget to update YOURDOMAIN with your actual domain name!

Create a Sitemap Page

The second step is to create a new file in the root directory of your website, I call mine sitemap.php. It should contain this:

<?php
    header('Content-type: application/xml');
    include('perch/runtime.php');
    perch_pages_navigation(array(
        'template'=>'sitemap.html',
        'flat'=>true
    ));
?>

You could do this as a new Perch page – but that’s unnecessary – it’s simpler to just create an independent new .php file.

This file (sitemap.php) calls the Perch runtime and then spits out the list of pages which make up your site – you can then submit your sitemap to Google via Webmaster Tools, or reference it in your robots.txt file like this:

User-agent: *
Allow: /
Sitemap: https://YOURDOMAIN.co.uk/sitemap.php

Again – don’t forget to enter your proper domain name.

Conclusions

This is pretty basic – but Google seems happy enough with it. As always, this is more a reference for my benefit that anyone elses, but if it helps you out feel free to make use of the code!

Jack Barber, freelance web developer based in Whitby, UK

Written By

I'm a freelance web developer based in Whitby, UK. I built my first website using GeoCities, and learned to write HTML and CSS using Notepad. Web technology has come a long way since then, as have my web development skills!

These days I love helping my clients make the most of the internet. I provide design, development, marketing and IT support services, forming long-term partnerships with my clients.

Connect With Me

Posted in Uncategorized