October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
Mountain View desk4 min

How I Built a PHP Sitemap to Help Google Find My Pages

A PHP sitemap can help Google discover your canonical pages, but it cannot force indexing. Here’s how to generate, submit, and troubleshoot one.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

I built a PHP-generated XML sitemap to give Google a reliable list of the pages I wanted it to discover. It helped address my site’s indexation problem, but a sitemap is a discovery aid—not a way to force Google to index URLs. Google says submitting one is only a hint, and it may not download the file or crawl every listed page.

What a PHP sitemap can—and cannot—fix

A sitemap lists the URLs a site owner considers canonical and wants search engines to discover. For a PHP application, XML is a flexible, widely supported format. Google can also find pages through crawlable links, so a sitemap supplements a sound site structure rather than replacing it.

As an Amazon Associate I earn from qualifying purchases.

In my case, I used the sitemap to make the intended URLs explicit and give Google another discovery path. The title reflects my experience; attributing any indexing change to the sitemap alone requires checking dated Search Console records, server logs, and changes made to the site at the same time. A sitemap does not guarantee crawling, indexing, ranking, or faster inclusion. Google describes submission as a hint, not a command: Google’s sitemap guidance.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How to create a sitemap in PHP

Start with the application’s actual published, canonical pages. Exclude duplicate URLs, noncanonical variants, and pages you do not want in search results. Then emit valid XML containing fully qualified absolute URLs. PHP’s XMLWriter extension is a practical way to generate XML without assembling markup by hand; it supports streaming output as well as file generation. See the PHP XMLWriter documentation.

Example: write a basic sitemap file

This example assumes $urls contains only published canonical URLs from your application. Replace the sample array with a query or data source appropriate to your site.

<?php
$urls = [
    'https://example.com/',
    'https://example.com/about/',
];

$writer = new XMLWriter();
$writer->openURI(__DIR__ . '/sitemap.xml');
$writer->startDocument('1.0', 'UTF-8');
$writer->setIndent(true);
$writer->startElement('urlset');
$writer->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');

foreach ($urls as $url) {
    $writer->startElement('url');
    $writer->writeElement('loc', $url);
    $writer->endElement();
}

$writer->endElement();
$writer->endDocument();
$writer->flush();

In production, obtain URLs from your content database or routing layer and ensure each is absolute, publicly accessible, and canonical. XMLWriter escapes element content; still validate the resulting file and confirm the URL source cannot introduce malformed or unintended entries. Google’s format requirements are in its sitemap documentation.

Respect the file limits

Google allows up to 50,000 URLs or 50 MB uncompressed in a single sitemap. If your URL set exceeds either limit, generate multiple sitemap files and publish a sitemap index that points to them. These are documented operational limits, not estimates of how many pages Google will index.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose a format that fits the site

XML is a versatile choice for an application-generated sitemap. A plain-text sitemap can be sufficient for a list of page URLs, while RSS or Atom feeds can be useful for feeds or recent URLs. A small site with only a few dozen stable pages may be manageable manually; automatic generation becomes more maintainable as pages change or multiply. Whichever format you choose, keep the URL set accurate.

How to submit the sitemap to Google

  1. Publish the file. Put the generated sitemap at a stable, publicly reachable URL, such as https://example.com/sitemap.xml. Request it over HTTP and check that it returns the XML—not a login page, access challenge, error, or unrelated application output.
  2. Submit it in Search Console. Open the relevant property in Google Search Console, go to Sitemaps, enter the sitemap URL, and submit it. Review the report for fetch and processing errors.
  3. Optionally list it in robots.txt. Add a sitemap directive with its absolute URL, for example Sitemap: https://example.com/sitemap.xml. Google documents both submission and discovery methods in its sitemap guidance.

Submitting the file does not ensure Google will fetch it, crawl its URLs, or include them in search results. Google’s indexing FAQ likewise makes clear that a sitemap does not guarantee indexing.

Why pages may still not be indexed

A sitemap can make URLs known to Google without resolving the reason those URLs are absent from search results. Work through the failure points rather than treating every case as a discovery problem.

  • Invalid or unintended URLs: Check that each listed URL resolves successfully and matches the canonical version you want Google to consider. Remove duplicates and noncanonical variants.
  • Sitemap access or processing errors: Check the Search Console sitemap report, then request the sitemap directly. Confirm the server returns the expected XML and does not require authentication or expose an access challenge.
  • Crawl restrictions: Review robots.txt and any access controls. Ensure the intended pages can be fetched by Googlebot.
  • Weak discovery paths: Link to important pages from other crawlable pages on your site. Google can discover URLs through links; a sitemap is not a substitute for navigable internal links.
  • Server problems: Inspect server logs and availability around Googlebot requests. Capacity limits, errors, or unstable responses can interfere with crawling.
  • Indexing decisions: A page can be crawled and still not appear in search results. Use Search Console URL Inspection to distinguish a discovery or crawling issue from an indexing decision, then review the page and its canonical signals. Google’s crawling troubleshooting guidance outlines relevant checks.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When a sitemap is useful

Sitemaps are especially useful for large, new, or media-rich sites, where a clear URL list can supplement other discovery routes. A small site with pages linked clearly from one another may not need a sitemap. Google’s sitemap overview explains when they can help and the formats Google supports.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Do not substitute the Indexing API for a general sitemap. Google recommends that API for specific short-lived job posting and livestream URLs; it is not intended as a general way to submit ordinary site pages. Keep a sitemap for broader site coverage. See Google’s Indexing API guidance.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.