How to create a sitemap.xml with your own code (Sitemap index file)

Creating a sitemap.xml file with Python code step by step to help search engines know our web pages. There is an example for a static website that uses .html and .php files, along with instructions on how to use it and send it to Google & Bing via Search Console or ping URL.

Creating a sitemap.xml file with Python code to help search engines know our web pages. There is an example for a static website that uses .html and .php files, along with instructions on how to use it and send it to Google & Bing via Search Console or ping URL.

A sitemap is a file that compiles the URLs of important pages on your website, helping search engines understand and navigate your pages more easily, which is good for SEO and increasing traffic. Here's how to create and submit a sitemap to Google for maximum efficiency.

  • Sitemap: It is an XML file that tells search engines like Google which pages are on our website.
  • Sitemap Index: It is an XML file that combines multiple sub-sitemaps, making it easier to manage large websites.
  • <loc> = URL of web page
  • <lastmod> = Last updated date (format YYYY-MM-DD)
  • <changefreq> = Expected update frequency (always, hourly, daily, weekly, monthly, yearly, never)
  • <priority> = Importance value (0.0 – 1.0)

This code will read a list of URLs and write a sitemap.xml file for you.

If your website is Static website (plain HTML) And there are many pages such as

/index.html  
/about.html  
/contact.html  
/blog.html  
/products/product1.html  
/products/product2.html
Python code sample to create a Sitemap for a Static website

This code will scan all .html files in your web folder and automatically write a sitemap.xml.

🔹 Usage

  1. Put this code in your project folder (eg. generate_sitemap.py)
  2. Run with python generate_sitemap.py
  3. You will get the file sitemap.xml Located at the root (public_html/sitemap.xml)
  4. Open in browser → https://yourdomain.com/sitemap.xml
  5. Submit to Google & Bing (use Search Console or ping URL)

If your website is a static website that uses .php files (such as index.php, about.php, contact.php), the method for creating a sitemap.xml is similar to the .html case, except that we have to pull the .php file instead.

/index.php
/about.php
/contact.php
/blog.php
/products/product1.php
/products/product2.php
Python code for creation sitemap.xml From file .php

🔹 Usage

  1. Place this script file in your project folder (e.g. generate_sitemap.py)
  2. Run with python generate_sitemap.py
  3. You will get the file sitemap.xml Located at the root (public_html/sitemap.xml)
  4. Try opening it in a browser → https://yourdomain.com/sitemap.xml
  5. Take it away Submit to Google Search Console / Bing Webmaster

If you are using Shared Hosting, which is limited by the fact that you are not allowed to run Python files directly on shared hosting.

Install Python on your machine (Windows/Mac)

Run the Python script → you will get a file sitemap.xml

Open Command Prompt (Windows) or Terminal (Mac/Linux) and type:

Upload file sitemap.xml Go to public_html/ of hosting through File Manager / FTP

Go to the browser →

If you don't want to run Python locally, you can write PHP code to automatically generate a sitemap.xml.

🔹 Usage
  • Save as file generate_sitemap.php
  • Upload to public_html/
  • Call files via the web, such as:
https://yourdomain.com/generate_sitemap.php
  • You will get the file sitemap.xml Immediately at root

You want to give When a new page is added (.php)sitemap.xml It is updated automatically, since Python cannot be run directly, we will use a PHP script to check the .php file in public_html every time and write a new sitemap.xml.

🔹 How to use

  • Upload the generate_sitemap.php file to public_html.
  • Call the file in the browser:
  • → It will regenerate (or update) the sitemap.xml every time.
  • When adding a new .php file → just run this file again and it will update the sitemap.

🔹Make it truly Auto (set up a Cron Job, no need to press it yourself)

You can use Hosting's Cron Job, for example, to run generate_sitemap.php daily/weekly.

In Hosting hPanel:

  • Go to Advanced → Cron Jobs
  • Add a Cron Job such as: php /home/username/public_html/generate_sitemap.php
  • Then set the time to Once a day

It is recommended to manage Sitemaps using a Sitemap index file. When a Sitemap exceeds the size limit, it is recommended to split it into multiple files and use the index file to submit up to 500 files per site simultaneously in Search Console. The XML format of the index file is similar to a normal Sitemap and must be located in the same directory or deeper.

More information https://developers.google.com

What is Sitemap.xml?

An XML file that tells Google and search engines which pages are included, along with ranking information such as last modified date (lastmod), change frequency (changefreq), and priority (priority).

Is a sitemap.xml required?

It's not required, but it helps search engines index your pages faster, especially for larger sites or sites with frequent new pages added.

How is Sitemap.xml different from robots.txt?

robots.txt → tells search engines which pages to block access; sitemap.xml → tells search engines which pages are included and should be indexed.

Can PHP/HTML websites use sitemap.xml?

Sure! Sitemap.xml doesn't care if the file is PHP, HTML, or a dynamic URL, it just tells you the URL of the web page.

How often should I update my sitemap.xml?

If new pages are added frequently → you should update them immediately * If the website is stable → update when there are changes

Is sitemap.xml too large?

Maximum XML file size = 50MB, maximum 50,000 URLs per file. If more than this → split into multiple files and create a sitemap index.

Can dynamic URLs (such as query strings) be included in sitemaps?

You can, but you should use URLs that Google can actually index and avoid URLs that create duplicate content.

How to automatically generate a sitemap.xml?

Use PHP script (suitable for Shared Hosting) Use Python / External Script and upload it Use Plugin / CMS such as WordPress has a generator provided.
Facebook Comments Box
Previous Article

IUX Review Info: Forex Start 10$ Withdraw 5$ and Beginner's Guide