Generate Dynamic Sitemap With PHP Without Database
Generating a sitemap for your website is an important aspect of search engine optimization. It helps search engines like Google, Bing, and Yahoo to index your website's content more efficiently. Having a sitemap ensures that all of the pages on your website are properly crawled and indexed. In this post, we will discuss how to generate a dynamic sitemap using PHP without a database
Create Dynamic Sitemap Using PHP
A sitemap is a file that contains a list of all the pages on your website. The sitemap helps search engines to navigate your website and index its pages. A dynamic sitemap is a sitemap that is generated automatically based on the content of your website. A dynamic sitemap is useful when you have a large website or when you frequently add new content to your website. To generate a dynamic sitemap, we will use PHP. PHP is a server-side scripting language that can be used to generate HTML, XML, and other types of files. We will use PHP to generate an XML sitemap file.
Code To Generate Sitemap Using PHP
<?php
header('Content-type: application/xml');
$baseurl = "https://www.narendradwivedi.org/"; // Enter Your URL
// NarendraDwivedi.Org
$output = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$output .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
echo $output;
$path="./";
$files=opendir($path);
if ($files) {
while (($file_name=readdir($files)) !==FALSE) {
if ($file_name !="." && !(is_dir($file_name))) {
$file_name_new = $file_name;
$file_last_modified = filemtime($path.$file_name);
echo "<url>";
echo '<loc>'.$baseurl.$file_name_new.'</loc>';
echo '<lastmod>'. date("Y-m-d\TH:i:s\Z", $file_last_modified).'</lastmod>';
echo '</url>';
}
}
}
echo "</urlset>";
?>
In this code , the sitemap.php will generate sitemap of all the files (not folder) exist in same directory in which sitemap.php exist.PHP Code To Dynamically Generate Sitemap Of Files Existing In Other Folder
To include files of a specific folder , change the $path value with the folder and change the $baseurl with $baseurl.$path . For example $path="myfolder/"
Complete Code
<?php
header('Content-type: application/xml');
$baseurl = "https://www.narendradwivedi.org/"; // Enter Your URL
// NarendraDwivedi.Org
$output = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$output .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
echo $output;
$path="myfolder/";
$baseurl=$baseurl.$path;
$files=opendir($path);
if ($files) {
while (($file_name=readdir($files)) !==FALSE) {
if ($file_name !="." && !(is_dir($file_name))) {
$file_name_new = $file_name;
$file_last_modified = filemtime($path.$file_name);
echo "<url>";
echo '<loc>'.$baseurl.$file_name_new.'</loc>';
echo '<lastmod>'. date("Y-m-d\TH:i:s\Z", $file_last_modified).'</lastmod>';
echo '</url>';
}
}
}
echo "</urlset>";
?>
Conclusion
In this way , we can create sitemap for all the files which exists on our server
FAQ
How to generate dynamic sitemap with php
To generate dynamic sitemap with php , you can create a php page which generate xml code including all the pages exists on server
Can we create dynamic sitemap without php
Yes , we can create sitemap using php without using database like mysql
Which timezone is used in this code
We have used Z (zulu) timezone. You can use any other timezone according to your need
3 comments