Quick answer: How to auto-update a PHP XML sitemap

Create a sitemap.php file that finds published PHP pages and outputs valid XML each time it is requested. Then route the public /sitemap.xml URL to that PHP script using Apache rewrite rules. New PHP articles can appear automatically without editing a static XML file after every publication.

This approach works for a small core-PHP website with page files in known folders. It must exclude admin pages, redirects, noindex content and helper scripts. A sitemap helps discovery but does not guarantee indexing.

Step 1: Create sitemap.php in your website root

The minimal example below scans only one blog folder. Unlike a generic recursive crawler, it deliberately restricts filenames and confirms that the file contains article metadata.

<?php
declare(strict_types=1);
header('Content-Type: application/xml; charset=UTF-8');
$host = 'https://example.com';
$urls = [];
foreach (glob(__DIR__ . '/blog/*.php') ?: [] as $file) {
    $slug = basename($file, '.php');
    if ($slug === 'index' || !preg_match('/^[a-z0-9-]+$/', $slug)) {
        continue;
    }
    $code = file_get_contents($file, false, null, 0, 10000);
    if (!$code || !preg_match('/\$(?:title|pageTitle)\s*=/', $code)) {
        continue;
    }
    $urls[] = $host . '/blog/' . $slug;
}
$xml = new SimpleXMLElement('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"/>');
foreach ($urls as $url) {
    $node = $xml->addChild('url');
    $node->addChild('loc', htmlspecialchars($url, ENT_XML1));
}
echo $xml->asXML();

Important: this is a teaching example. Production code should also check canonical targets, redirects and indexability; a full site may have many nested directories.

Step 2: Route sitemap.xml to PHP

For an Apache server with mod_rewrite, add this line near the top of the website's .htaccess inside its RewriteEngine block:

RewriteRule ^sitemap\.xml$ sitemap.php [L,NC]

Keep your robots.txt sitemap reference pointing to https://example.com/sitemap.xml. Open that URL in a browser and check that it returns XML, not PHP source code, a 404 page or an HTML error page.

Step 3: Test the sitemap before submission

  1. Create one sample published article in /blog/ and refresh /sitemap.xml.
  2. Confirm the new canonical article URL appears only once.
  3. Check that /blog/index, redirected pages and drafts are absent.
  4. Submit or verify the sitemap in Google Search Console → Sitemaps.
  5. Change the actual page content and update lastmod only when there is a meaningful modification.

Google recommends including canonical URLs in sitemaps. See its official sitemap guide. If your pages still have no exposure after indexing, use our indexed but zero impressions checklist.

Production checklist before indexing hundreds of URLs

For real websites, the discovery rules should match the site architecture. A redirect-only page should not appear, because its canonical destination belongs in the sitemap instead. A page marked noindex should not be advertised as an indexable URL, and a helper script such as config.php must be excluded even when it lives near published articles.

Use truthful lastmod values for meaningful content changes, not every file copy or deployment. Keep URLs on the canonical HTTPS host, and validate XML after every sitemap script change. Our website redesign SEO guide covers redirect and canonical risks.

Frequently asked questions

Do I need a cron job for this PHP sitemap?

No. The script runs when requested, although your host or CDN may cache the response.

Can the sitemap include clean URLs without .php?

Yes, if your server already serves those extensionless URLs correctly.

Does a sitemap guarantee Google indexing?

No. It helps discovery; Google makes separate crawling and indexing decisions.

For core PHP websites built to support search, see web development and technical SEO at MTP DigiTech.

NEED HELP?

Need help with PHP or technical SEO?

MTP DigiTech builds PHP websites with clean URLs, secure server configuration and crawlable pages.

Talk to a Developer →