Wiki

Clone wiki

Perch Resource Wiki / Perch XML Sitemap

XML Sitemap

This example comes from the excellent website by Clive Walker. The full tutorial can be found here.

Steps

  1. Create sitemap.php in route
  2. Create navigation template sitemap.html
  3. Set sitemap as desired with categpries, cub pages and blog if required
  4. Add rewrite rules to .htaccess
  5. Add robots.txt file

Step 1

The first step is to create sitemap.php in your /route directry.

Clives suggestion for the content of this is as follows:

<?php
 header('Content-type: application/xml');
 include('perch/runtime.php');
 echo '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
perch_pages_navigation(array(
     'template' => 'sitemap.html',
     'add-trailing-slash' => true,
     'flat' => true,
     'hide-extensions' => true
 ));
perch_pages_navigation(array(
     'navgroup' =>'secondary-links',
     'template' => 'secondary-links-sitemap.html',
     'flat' => true,
     'hide-extensions' => true
 )); 
     perch_blog_custom(array(
     'template' => 'sitemap-blog.html',
     'sort'=>'postDateTime',
     'sort-order'=>'DESC',
     'count' => 3000
 ));
 perch_blog_categories(array(
     'sort' => 'catSlug',
     'sort=order'=> 'ASC',
     'template'=> 'sitemap-category.html'
 ));
 echo '</urlset>';
 ?>

As you can see, each section refers to diffrent parts of your site that you may wish to have in your sitemap. Clive has broke his into the following sections:

  1. sitemap.html Main Pages
  2. secondary-links-sitemap.html Secondary links
  3. sitemap-blog.html Blogs
  4. sitemap-category.html Blog categories

Step 2

Now you are required to create each section that you require and place in the correct directry.

Main Pages

sitemap.html

<url>
 <loc>http://www.YOURDOMAIN.co.uk<perch:pages id="pagePath" /></loc>
 <changefreq>monthly</changefreq>
 <priority>1.00</priority>
</url>

Location: /perch/navigation/

secondary-links-sitemap.html

This may not be required. See Clive's original Blog Article for a full explaination.

Step 3

Blogs

sitemap-blog.html

If you are running a blog, you may wish to include your blog articles in the sitemap.

<url>
 <loc>http://www.YOURDOMAIN.co.uk<perch:blog id="postURL" /></loc>
 <changefreq>monthly</changefreq>
 <priority>0.80</priority>
</url>

Location: /perch/templates/blog

Blog categories

sitemap-category.html

Again, you are running a blog, you may wish to include your blog categories in the sitemap.

<url>
 <loc>http://www.YOURDOMAIN.co.uk/blog/category/<perch:category id="catSlug" /></loc>
 <changefreq>monthly</changefreq>
 <priority>0.60</priority>
</url>

Location: /perch/templates/blog

Step 4

We now need to to add some rewrite rules to the .htaccess file, so that when a search engine looks for your sitemap it sees sitemap.xml rather then sitemap.php.

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule ^blog/([a-zA-Z0-9-/]+)$ /blog/post.php?s=$1 [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}.php -f
  RewriteRule ^(.+)$ $1.php [L,QSA]
  RewriteRule (.*).xml(.*) $1.php$2 [nocase]
</IfModule>

The important line here is RewriteRule (.*).xml(.*) $1.php$2 [nocase]. A full explaination on the other lines can be found on the Rewrite Rules page.

Step 5

The robots.txt file lets a serach know what they are allowed and not aloud to crawl on your site. A basic robot.txt would look simular to the following:

User-agent: *
Allow: /

Sitemap: https://www.YOURDOMAIN.co.uk/sitemap.xml
Location: /

Updated