Sitemap explanation and free PHP script

Afbeelding Sitemap explanation and free PHP script

A sitemap is essentially a map of your website in one file. Just like a city map helps you find streets and landmarks, a sitemap helps search engines discover and understand all the pages on your site. Without a sitemap, a crawler may miss some pages, especially if the internal links are not perfect. With a clear sitemap, you ensure that every important page is found and indexed.

What exactly is a sitemap?

There are two main types of sitemaps. The first is the XML sitemap, intended for search engines. It contains a list of all URLs, each with additional information such as the last modification date and priority. The second is the HTML sitemap, which helps visitors navigate. Both are valuable, but for SEO it is mainly the XML version that matters.

Why are sitemaps important for SEO?

Search engines like Google use crawlers to index your website. They follow links on pages to discover new URLs. If links are hidden, deeply nested in folders or dynamically generated, a crawler can miss them. An XML sitemap gives a crawler a complete overview, so that even deeply linked or new pages are indexed quickly.

In addition, you can indicate in the sitemap how often a page changes. If you regularly publish blog posts, you can advise search engines to check those pages more often. A lower frequency is fine for static pages. This way, your server resources are used efficiently and your site stays up-to-date in the search results.

The benefits at a glance

First and foremost, you speed up the discovery and indexing process. If a page is missing from the sitemap, there is a greater chance that it will not appear in Google. In addition, you can set priorities, so that your main parts, such as category pages or important landing pages, get priority. Internationally oriented sites also benefit: with a sitemap you can link language and region variants, so that search engines show the right version to users in the Netherlands, Belgium or elsewhere.

How do you create an XML sitemap?

In the simplest form, you create a file sitemap.xml in your root directory. The file starts with an XML declaration, followed by a list of <url> blocks. Below is a basic example:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.example.com/ </loc>
<lastmod>2025-05-12</lastmod> 
<changefreq>daily</changefreq> 
<priority>1.0</priority>
</url>
<url> 
<loc>https://www.example.com/blog/seo-tips</loc> 
<lastmod>2025-05-10</lastmod> 
<changefreq>weekly</changefreq> 
<priority>0.8</priority>
</url>
</urlset>

Make sure that each URL is exactly correct and that the file validates against the sitemap schema. You can use online tools to check this or install a plugin in your CMS that automatically updates the sitemap.

The HTML sitemap for visitors

In addition to the XML sitemap, you can create a simple HTML page with links to all important sections. For example, place this under /sitemap.html. This way, visitors will see a clear navigation, while search engines use the XML version to index the site.

Submit a sitemap to search engines

Have you uploaded your sitemap.xml? Then report it in Google Search Console under “Sitemaps” by entering the URL. Google will read the sitemap and show you which pages were crawled, which errors occurred and if any URLs are missing. For Bing, use Bing Webmaster Tools.

Maintenance and Maintenance

A sitemap is not a static file forever. Every time you add new pages or change content significantly, the sitemap needs to be updated. For large sites, this can be automated with a script or a CMS plugin. This way, the sitemap will always be up-to-date and indexing will continue smoothly.

Common mistakes

Some sites list URLs in the sitemap that are not accessible, such as deleted or moved pages. This leads to crawler errors and confusion. Therefore, regularly check Search Console for error messages and remove or update URLs in the sitemap. It is also important that the sitemap does not become too large; keep it under 50,000 URLs or split it into multiple sitemaps with an index file.

Free PHP Sitemap Script

Below we have placed a simple PHP Sitemap Script for you. Copy the code, place in an empty PHP file (for example: sitemap-generator.php) and upload it to the root of your domain (for example: https://www.yourdomain.com/sitemap-generator.php). Don't forget to replace our URL in the first sentence of the script with your own URL. Call the script via the web browser and it will create a sitemap of your website. The sitemap.xml file will then also be placed in the root of your domain.

Please note: This script automatically searches your entire website. It may take a while before it is ready, depending on the size of your website.

$baseUrl = 'https://www.zichtbareurl.nl'; // <-- Replace with your domain $startUrl = $baseUrl; $sitemapPath = __DIR__ . '/sitemap.xml'; $visited = []; $toVisit = [[$startUrl, 0]]; // Any URL with depth $depthMap = []; // Keeps track of the depth at which we find a URL function crawl($url, $depth) { global $baseUrl, $visited, $toVisit, $depthMap; if (isset($visited[$url])) return; $visited[$url] = true; $depthMap[$url] = $depth; echo "Visit (depth $depth): $url\n"; $context = stream_context_create(['http' => ['timeout' => 5]]); $html = @file_get_contents($url, false, $context); if ($html === false) return; preg_match_all('/]*href=["\']([^"\']+)["\']/i', $html, $matches); foreach ($matches[1] as $link) { $link = html_entity_decode($link); // Make absolute if (strpos($link, 'http') !== 0) { $link = rtrim($baseUrl, '/') . '/' . ltrim($link, '/'); } // Internal only if (strpos($link, $baseUrl) === 0 && !isset($visited[$link])) { // Skip unwanted links if (preg_match('/^(mailto:|javascript:|#)/', $link)) continue; if (preg_match('/\.(pdf|zip|jpg|png|css|js)$/i', $link)) continue; $toVisit[] = [$link, $depth + 1]; } } } // Crawl all URLs while (!empty($toVisit)) { list($url, $depth) = array_shift($toVisit); crawl($url, $depth); } // Priority function based on depth function calculatePriority($depth) { if ($depth == 0) return 1.0; if ($depth == 1) return 0.8; if ($depth == 2) return 0.6; returns 0.4; } // Generate sitemap $xml = new SimpleXMLElement(''); $xml->addAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'); foreach ($visited as $url => $_) { $depth = $depthMap[$url] ?? 3; $priority = calculatePriority($depth); $urlTag = $xml->addChild('url'); $urlTag->addChild('loc', htmlspecialchars($url)); $urlTag->addChild('lastmod', date('Y-m-d')); $urlTag->addChild('changefreq', 'weekly'); $urlTag->addChild('priority', number_format($priority, 1)); } $xml->asXML($sitemapPath); echo "Sitemap generated: $sitemapPath\n"; // Ping Google $pingUrl = 'https://www.google.com/ping?sitemap=' . urlencode($baseUrl . '/sitemap.xml'); $ch = curl_init($pingUrl); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 5, ]); $response = curl_exec($ch); curl_close($ch); echo $response !== false ? "Google pinged\n" : "Error with Google ping\n";

Conclusion

Sitemaps are the backbone of a good SEO strategy. They ensure that search engines find each important page, understand when and how often it needs to be updated, and what priority it has. By creating a proper XML sitemap, providing an HTML version for your visitors, and regularly checking your sitemap in Search Console, you lay a solid foundation for optimal indexing and better search engine rankings. Start setting up or improving your sitemap today and see how your website is actually picked up faster and more completely by search engines.


Did you find this useful? Share it with your network now!

Facebook | Twitter | LinkedIn | WhatsApp
Learn more about speed and performance
How a CDN can improve your SEO and how to set one up
Do you want to make your website faster and rank higher in Google? Then a CDN is a smart choice. In this article, we explain step by step what a CDN is, why it is important for SEO, and how you can use a CDN – both with an external partner and via your own subdomain...

Sitemap explanation and free PHP script
A sitemap is essentially a map of your website in one file. Just like a city map helps you find streets and landmarks, a sitemap helps search engines discover and understand all the pages on your site...

Score better in Google with asynchronous script loading
Every second counts on the internet. If a website loads slowly, people will click away...

What are Core Web Vitals and why are they important for your SEO
If you have a website, you want people to be able to find it quickly via Google. That's where SEO (search engine optimization) comes in...

Boost your SEO with the right server choice and opt for the perfect hosting
The hosting you choose directly affects the speed, uptime and security of your website. These technical factors are important ranking signals for search engines like Google...

Why optimized HTML makes your website fly
A fast, clean, and well-functioning website starts with the basics: HTML. It’s the backbone of your website...

The 10 biggest mistakes that are making your website slow
In the world of websites, speed is everything. A slow website drives away visitors, lowers conversions and is penalized by search engines like Google...

The best tools to measure your website speed
In today’s fast-paced online world, every millisecond counts. Visitors drop off if a website loads slowly, and search engines reward faster sites with better rankings...

Use the free SEO Checker

Most websites can easily be taken to a higher level by first getting the most basic SEO in order. My free SEO Checker checks for you whether your website meets these basic requirements, or whether there is still room for improvement.

Use my free SEO Checker now

Enter the URL of your website and see where you can improve your website. Please note: A 100% score only means that you have the basic SEO for the page in question in order. For more tips, I would like to invite you to read all my articles.







© 2025 VisibleURL.com. All rights reserved.

Also available in:

Dutch flag