Creating an XML Sitemap for a Dynamic PHP Website
When I first built the Jersey News Aggregator and the wider Auspice Darer website project, I concentrated on functionality. The site could collect news, display articles, search archives and present information exactly as intended. What it could not do was explain itself very well to search engines.
That changed when I began setting up Google Search Console and Google Analytics. One of the first recommendations was to submit an XML sitemap.
At first glance, an XML sitemap appears to be little more than a list of pages. In reality, it serves as a roadmap for search engines, helping them discover content that might otherwise take longer to find.
What Is an XML Sitemap?
An XML sitemap is a machine-readable file that lists the pages available on a website.
A typical sitemap entry looks something like this:
<url>
<loc>https://www.example.com/about.php</loc>
<lastmod>2026-05-01</lastmod>
</url>
The sitemap tells search engines:
Which pages exist
Where they are located
When they were last modified
Which content should be prioritised for crawling
Without a sitemap, search engines rely primarily on links to discover pages. For a small website this may not matter much. For larger or more dynamic projects, a sitemap can significantly improve content discovery.
Static Versus Dynamic Sitemaps
Many websites use a static sitemap.
This is simply a manually maintained XML file containing a list of pages.
The problem is obvious. Every time a page is added, removed or renamed, the sitemap must also be updated.
For websites that publish content regularly, this quickly becomes impractical.
In my case, the news aggregation systems are constantly changing. New articles arrive throughout the day, archives expand and new sections are added periodically.
A dynamic sitemap made much more sense.
Instead of maintaining the file manually, PHP generates the sitemap automatically whenever it is requested.
Generating a Sitemap with PHP
The basic process is straightforward.
First, tell the browser and search engines that the output is XML.
header('Content-Type: application/xml; charset=utf-8');
Next, generate the opening sitemap structure.
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
You can then loop through your pages and output each URL.
echo '<url>';
echo '<loc>https://www.example.com/index.php</loc>';
echo '</url>';
Finally, close the sitemap.
echo '</urlset>';
The result is a valid XML sitemap that search engines can read directly.
Deciding Which Pages to Include
One mistake I nearly made was trying to include everything.
Not every URL belongs in a sitemap.
Pages that are useful candidates include:
Home pages
Article pages
Archive pages
Category pages
Important static content
Pages that generally should not be included include:
Administrative interfaces
Internal APIs
Temporary development pages
Duplicate content
Search result pages
The goal is to guide search engines towards valuable content rather than every possible URL.
Dynamic Content and Large Websites
The benefits of dynamic generation become more obvious as a site grows.
For example, the Jersey News Aggregator now contains thousands of articles collected from multiple sources.
Generating the sitemap from the database ensures that new content automatically becomes discoverable without any manual intervention.
As archives continue to grow, additional sitemap files can be created and grouped together using a sitemap index.
This allows search engines to navigate large collections efficiently.
Submitting the Sitemap
Once the sitemap is accessible, it can be submitted through Google Search Console.
The process takes only a few moments.
Simply provide the sitemap URL, typically:
https://www.example.com/sitemap.xml
Google will then begin reading the sitemap and reporting any issues that it encounters.
Over time, Search Console can also provide useful information about indexing status and crawl activity.
Lessons Learned
Creating an XML sitemap turned out to be one of the simplest improvements I made to the project, yet it provided immediate benefits.
The sitemap acts as a bridge between the website and search engines, helping ensure that content can be discovered efficiently.
For small websites, a manually maintained sitemap may be perfectly adequate. For dynamic PHP websites, however, automatic generation is usually worth the small amount of development effort required.
As the Jersey News Aggregator and Auspice Darer projects continue to expand, the sitemap remains one of the most important pieces of infrastructure operating quietly behind the scenes.
It may not be visible to visitors, but it plays a crucial role in helping them find the site in the first place.

Comments
Post a Comment