This PHP tutorial helps to create a dynamic XML sitemap generator using PHP. We’ll create a php script that helps to generate a website sitemap in xml format.
A sitemap is a mapping file that contains information about the URLs of the site’s pages and other necessary details.
What is sitemap
Sitemaps are an easy way for webmasters to inform search engines about pages on their sites that are available for crawling.
A sitemap, in its most basic form, is an XML file that contains a list of the URLs for a website along with additional metadata for each URL.? You can get more information for here.
For your website, make a legitimate sitemap and submit it to Google. It will aid its bots in indexing the site and achieving high rankings.
Online XML generator
There is a number of online free and paid tools available to generate XML sitemap for your website.
Sitemap creation can be done online using the XML-Sitemaps Generator. It generates the best format for posting to search engines like Google, Bing, and others.
I am using a PHP libs to generate an XML sitemap for a website.
The XML sitemap is generated using the specified configuration parameters by an object-based PHP script. This is an open-source sitemap generator libs that automate creating a sitemap for Google crawling.
- Generate a sitemap for your website.
- Multiple options for generating sitemaps
- Option to only look through certain filetypes
- Parse all relative link types (
// , # , ?) and more
Generate XML Sitemap Using PHP
Let’s discuss generating sitemaps for a website.
How To Install Libs
Simply download the sitemap config and sitemap generator and add them to your project to install this script (same directory).
How Import Libs
After installing the script you can use the script by including it into your script
Generate XML Sitemap
Let’s create an instance of the class by calling the constructor. We’ll pass the config file as a parameter that’ll have sitemap configurable params like URL, file save location, date format, etc.
$config = include("sitemap-config.php"); $smg = new SitemapGenerator($config); // Run the generator $smg->GenerateSitemap();
Full Source Code:
We have created a folder
/test-xml and exported libs here and created an
index.php file. We'll add the below code into this file.
<?php include "sitemap-generator.php"; $config = include("sitemap-config.php"); $smg = new SitemapGenerator($config); // Run the generator $smg->GenerateSitemap(); ?>