As we already know, for creating XML Sitemap for a static website, there are a lot of online tools available out there. But here we are going to create a dynamic XML sitemap generator by using a simple PHP Script for Google and other search engines.
Why do we need this script instead of the tools that are already available?
Before I answer this, Let’s understand what is
Once you upload the sitemap_index.php in the
public_html directory. Each time a search engine crawler makes a request to get the updated sitemap of your website the script will generate the site map and return the updated sitemap. So even if you make any change on your website by creating a new page or directory the search engine will get the info of that via that XML sitemap.
If you use any other site map generator tool then you have to do it manually by creating and uploading the updated file every day for the search console. But this script will automate the work as it generates the Sitemap when search engine crawlers make requests to get the sitemap. Isn’t this amazing ?
If you want to know more about the sitemap and sitemap format visit sitemap.org
How to use this?
Usage is pretty straightforward, simply upload the sitemap_index.php in the
public_html directory. Now open this file and make these changes
$root = "https://yourdomain.com";
skip_diris the array containing all those directories that we don’t want to list in our sitemap.
skip_filesis the array containing all those files that we don’t want to list in our sitemap.
skip_file_extensionsis the array containing all the file extensions of the files that we don’t want to list in our sitemap.
$skip_dir = array( ".well-known", "cgi-bin" ); $skip_files = array( "sitemap_index.php", "test_codes.php" ); $skip_file_extensions = array( "html", "xml", "zip", "txt" );
Now open the
.htaccess file (create one if you don’t have it in the root directory) and insert these codes and save it.
RewriteEngine On RewriteRule ^sitemap_index\.xml/?$ sitemap_index.php
? Congratulations you have successfully created your own dynamic sitemap generator. To test this, Simply go to
https://yourdomain.com/sitemap_index.xml you can now submit this URL to search engines to provide an updated sitemap of your website each time to the search engine crawlers.