Blog Post

How to build a dynamic sitemap with PHP


The sitemap file for Coursedate, our online course booking system, is generated on the fly using PHP.

We don’t manually update the sitemap.xml file each time a training provider creates a new course. New course pages now appear automatically in the sitemap file as soon as they are published in Coursedate’s course directory. That’s less work for us and it means the sitemap is always up to date when search engines do their indexing.

Creating the sitemap using PHP is straightforward.

  1. Start with an empty file - sitemap.php
  2. Add the headers. We're telling the client that the content they've requested is xml.
                               
                               header("Content-Type: application/xml; charset=utf-8");
                   
                               echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; 
                         
                               
                            
  3. Start building the xml.
                               
    
                                  $xml = $xml . '<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/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
                               
                               
                            
  4. Add in the code that returns an array of published courses from the database. Loop through the code, adding a new element for each course. This is where you'd use your own data.
                               
                                  //got to do the looping here
                                  if(isset($courses) && count($course) > 0){
                                     $lastclient = 0;
                                     foreach($courses as $course){
                                        if($lastclient != $course['clientid']){
                                              //do the client login page
                                              $xml .= '<url>
                                                 <loc>' . $serverurl . $course['client_slug']. '/login</loc>
                                                 <changefreq>weekly</changefreq>
                                              </url>';                
                                              //do the client course page
                                              $xml .= '<url>
                                                 <loc>' . $serverurl . $client['course_slug']. '</loc>
                                                 <changefreq>weekly</changefreq>
                                              </url>';
                                        }
                                        $xml .= '<url>
                                                 <loc>' . $serverurl . $course['client_slug'] . '/course-directory/' . $course['id'] . '/' . getUrlForSEO($course['title']) . '</loc>
                                                 <changefreq>weekly</changefreq>
                                              </url>';
                                        $lastclient = $course['client_id'];
                                     }
                            
                                  }
                                  //end of looping  
                                   
                            
  5. Finish off the xml.
                               
                                  $xml .= '<url>
                                     <loc>' . $serverurl . 'login</loc>
                                     <changefreq>weekly</changefreq>
                                  </url>';
                      
                                  $xml .= '<url>
                                              <loc>' . $serverurl . 'coursedate-terms-conditions</loc>
                                              <changefreq>weekly</changefreq>
                                        </url>
                                        <url>
                                              <loc>' . $serverurl . 'coursedate-privacy-policy</loc>
                                              <changefreq>weekly</changefreq>
                                        </url>';
                      
                                  $xml .= '</urlset>';
                               
                            
  6. Modify .htaccess on the web server - we redirect requests for sitemap.xml to sitemap.php

You should definitely test your sitemap after you’ve made the edit to .htaccess, just try out one of the free xml sitemap validators on the web. You should also submit your sitemap to Google through the Google Search Console - Google will also validate your file and let you know if there is a problem.



Related Posts