Create a simple and basic Sitemap Index sitemap.xml
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.
1. Understanding Sitemaps and Sitemap Indexes
- 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.
2. Standard structure of sitemap.xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://yourdomain.com/</loc>
<lastmod>2025-08-17</lastmod>
<changefreq>weekly</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>https://yourdomain.com/about</loc>
<lastmod>2025-08-10</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
</urlset>
<loc>
= URL of web page<lastmod>
= Last updated date (formatYYYY-MM-DD
)<changefreq>
= Expected update frequency (always, hourly, daily, weekly, monthly, yearly, never
)<priority>
= Importance value (0.0 – 1.0)
3. Example Python code for creation sitemap.xml
This code will read a list of URLs and write a sitemap.xml file for you.
import datetime
domain = "https://yourdomain.com"
pages = [
"/",
"/about",
"/contact",
"/products",
"/blog"
]
today = datetime.date.today().isoformat()
sitemap_path = "sitemap.xml"
with open(sitemap_path, "w", encoding="utf-8") as f:
f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
f.write('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n')
for page in pages:
f.write(" <url>\n")
f.write(f" <loc>{domain}{page}</loc>\n")
f.write(f" <lastmod>{today}</lastmod>\n")
f.write(" <changefreq>weekly</changefreq>\n")
f.write(" <priority>0.8</priority>\n")
f.write(" </url>\n")
f.write("</urlset>\n")
print("Sitemap index created successfully!")
4. Create a Sitemap for a Static Website
4.1 For (plain .HTML files) static pages
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
import os, datetime
domain = "https://yourdomain.com"
web_dir = "./public_html"
today = datetime.date.today().isoformat()
urls = []
for root, dirs, files in os.walk(web_dir):
for file in files:
if file.endswith(".html"):
rel_path = os.path.relpath(os.path.join(root, file), web_dir)
url = "/" + rel_path.replace("\\", "/")
if url.endswith("index.html"):
url = url.replace("index.html", "")
urls.append(url)
sitemap_path = os.path.join(web_dir, "sitemap.xml")
with open(sitemap_path, "w", encoding="utf-8") as f:
f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
f.write('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n')
for url in urls:
f.write(" <url>\n")
f.write(f" <loc>{domain}{url}</loc>\n")
f.write(f" <lastmod>{today}</lastmod>\n")
f.write(" <changefreq>monthly</changefreq>\n")
f.write(" <priority>0.5</priority>\n")
f.write(" </url>\n")
f.write("</urlset>\n")
print("Sitemap index created successfully!")
This code will scan all .html files in your web folder and automatically write a sitemap.xml.
🔹 Usage
- Put this code in your project folder (eg.
generate_sitemap.py
) - Run with
python generate_sitemap.py
- You will get the file
sitemap.xml
Located at the root (public_html/sitemap.xml
) - Open in browser →
https://yourdomain.com/sitemap.xml
- Submit to Google & Bing (use Search Console or ping URL)
4.2 For static websites that use .php files (static pages)
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
import os, datetime
domain = "https://yourdomain.com"
web_dir = "./public_html"
today = datetime.date.today().isoformat()
urls = []
for root, dirs, files in os.walk(web_dir):
for file in files:
if file.endswith(".php"):
rel_path = os.path.relpath(os.path.join(root, file), web_dir)
url = "/" + rel_path.replace("\\", "/")
if url.endswith("index.php"):
url = url.replace("index.php", "")
urls.append(url)
sitemap_path = os.path.join(web_dir, "sitemap.xml")
with open(sitemap_path, "w", encoding="utf-8") as f:
f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
f.write('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n')
for url in urls:
f.write(" <url>\n")
f.write(f" <loc>{domain}{url}</loc>\n")
f.write(f" <lastmod>{today}</lastmod>\n")
f.write(" <changefreq>monthly</changefreq>\n")
f.write(" <priority>0.5</priority>\n")
f.write(" </url>\n")
f.write("</urlset>\n")
print("Sitemap index created successfully!")
🔹 Usage
- Place this script file in your project folder (e.g.
generate_sitemap.py
) - Run with
python generate_sitemap.py
- You will get the file
sitemap.xml
Located at the root (public_html/sitemap.xml
) - Try opening it in a browser →
https://
yourdomain
.com/sitemap.xml - 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.
So, there are two options for automatically generating sitemap.xml.
🔹 Option 1 (easiest) — Use your local machine to create and upload.
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:
python generate_sitemap.py
Upload file sitemap.xml
Go to public_html/
of hosting through File Manager / FTP
Go to the browser →
https://yourdomain.com/sitemap.xml
🔹 Option 2 — Use PHP to create a Sitemap on your Hosting.
If you don't want to run Python locally, you can write PHP code to automatically generate a sitemap.xml.
<?php
$domain = "https://yourdomain.com";
$web_dir = __DIR__; // public_html
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($web_dir));
$urls = [];
foreach ($files as $file) {
if ($file->isFile() && pathinfo($file, PATHINFO_EXTENSION) === "php") {
$path = str_replace($web_dir, "", $file->getPathname());
$url = str_replace("\\", "/", $path);
if (basename($url) === "index.php") {
$url = str_replace("index.php", "", $url);
}
$urls[] = $url;
}
}
$today = date("Y-m-d");
$sitemap = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
foreach ($urls as $url) {
$sitemap .= " <url>\n";
$sitemap .= " <loc>{$domain}{$url}</loc>\n";
$sitemap .= " <lastmod>{$today}</lastmod>\n";
$sitemap .= " <changefreq>monthly</changefreq>\n";
$sitemap .= " <priority>0.5</priority>\n";
$sitemap .= " </url>\n";
}
$sitemap .= "</urlset>";
file_put_contents($web_dir . "/sitemap.xml", $sitemap);
echo "✅ Sitemap created at {$domain}/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
6. There is a new sitemap.xml page that is automatically updated.
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.
PHP Code: Auto Generate Sitemap Every Time It Runs
<?php
$domain = "https://yourdomain.com";
$web_dir = __DIR__; // public_html
$today = date("Y-m-d");
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($web_dir));
$urls = [];
foreach ($files as $file) {
if ($file->isFile() && pathinfo($file, PATHINFO_EXTENSION) === "php") {
$path = str_replace($web_dir, "", $file->getPathname());
$url = str_replace("\\", "/", $path);
if (basename($url) === "index.php") {
$url = str_replace("index.php", "", $url);
}
$urls[] = $url;
}
}
// sitemap.xml
$sitemap = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
foreach ($urls as $url) {
$sitemap .= " <url>\n";
$sitemap .= " <loc>{$domain}{$url}</loc>\n";
$sitemap .= " <lastmod>{$today}</lastmod>\n";
$sitemap .= " <changefreq>monthly</changefreq>\n";
$sitemap .= " <priority>0.5</priority>\n";
$sitemap .= " </url>\n";
}
$sitemap .= "</urlset>";
file_put_contents($web_dir . "/sitemap.xml", $sitemap);
echo "✅ Sitemap updated at {$domain}/sitemap.xml";
?>
🔹 How to use
- Upload the generate_sitemap.php file to public_html.
- Call the file in the browser:
https://yourdomain.com/generate_sitemap.php
- → 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
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