XML sitemaps just turned 18 in June, and in honor of its full-fledged introduction into adulthood, I’m breaking down how XML sitemaps work, how to build one, and why XML sitemaps offer a boatload of SEO benefits.
What is an XML sitemap?
In a nutshell, an XML sitemap is a protocol in XML format that tells search engines about the URLs on a website available to crawl.
XML sitemaps were meant to share information about each URL, like:
- When it was last updated.
- How often it changes.
- How important it is in relation to other URLs across your website.
XML sitemaps allow search engines to crawl your website more efficiently if certain URLs are isolated.
If you’re an SEO professional, you probably already use XML sitemaps built into your Yoast WordPress plugin. But there are plenty of extras that you might be missing.
How to build a dynamic XML sitemap
If you’re using WordPress, leverage SEO plugins like Yoast and RankMath to generate a dynamic XML sitemap.
If you’re not using WordPress, follow these steps to build a dynamic XML sitemap.
Understand your navigational structure
Pull together a list of all our URLs on the website and group them to align to navigational structure and content groupings.
Partner with your developer team
Share the list of URLs grouped together with your dev team to gain their help in creating a dynamic XML sitemap using Python, PHP, or JavaScript.
Here is an example of how to code a dynamic XML sitemap using Python:
import xml.etree.ElementTree as ET
import datetime
# List of URLs (replace these with your actual URLs)
urls = [
"https://www.example.com/page1",
"https://www.example.com/page2",
"https://www.example.com/page3",
# Add more URLs here
]
# Create the root element
urlset = ET.Element("urlset")
urlset.set("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")
# Loop through URLs and create URL entries
for url in urls:
url_elem = ET.SubElement(urlset, "url")
loc_elem = ET.SubElement(url_elem, "loc")
loc_elem.text = url
lastmod_elem = ET.SubElement(url_elem, "lastmod")
lastmod_elem.text = datetime.datetime.now().isoformat()
changefreq_elem = ET.SubElement(url_elem, "changefreq")
changefreq_elem.text = "daily" # You can set this to the desired frequency
priority_elem = ET.SubElement(url_elem, "priority")
priority_elem.text = "0.5" # You can set this to a value between 0 and 1
# Create the XML tree
tree = ET.ElementTree(urlset)
# Save the XML to a file
tree.write("sitemap.xml", encoding="utf-8", xml_declaration=True)
When working with developers, I’ve leveraged Flask and Python together to create a truly dynamic XML sitemap that changes whenever the website’s content changes.
If we can define the dynamic sitemap route based on the website’s content using a function like get_dynamic_urls()
, we should be able to retrieve all URLs.
@app.route('/sitemap.xml', methods=['GET'])
def sitemap():
urls = get_dynamic_urls() # Replace with your dynamic URL retrieval logic
urlset = ET.Element("urlset")
urlset.set("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")
for url in urls:
url_elem = ET.SubElement(urlset, "url")
loc_elem = ET.SubElement(url_elem, "loc")
loc_elem.text = url
lastmod_elem = ET.SubElement(url_elem, "lastmod")
lastmod_elem.text = datetime.datetime.now().isoformat()
changefreq_elem = ET.SubElement(url_elem, "changefreq")
changefreq_elem.text = "daily"
priority_elem = ET.SubElement(url_elem, "priority")
priority_elem.text = "0.5"
xml_content = ET.tostring(urlset, encoding="utf-8")
return xml_content, {'Content-Type': 'application/xml'}
If you’re sharing code across multiple domain properties, ensure the files are coded properly on your HTML pages.
For example, your CSS and JavaScript files should be linked using and
tags.
Before sharing the code across different domains, you’ll need to create a centralized server to host the shared XML sitemap rules.
You will have multiple phases of your dynamic XML sitemap with your developers until all the rules get confirmed.
Don’t forget to check the dynamic URLs are actually getting retrieved and integrated into your sitemap logic.
Upload to your server
Once your XML sitemap is compressed, upload your dynamic XML sitemap to your website’s root directory.
Submit to search engines
Let search engines know about your XML sitemaps by submitting them through Google Search Console and Bing Webmaster Tools.
Ongoing maintenance
An XML sitemap is not just a “set it and forget it” SEO tactic. It’s essential to update your XML sitemap as your website changes.
15 tips to keep in mind when building an XML sitemap
1. XML sitemaps are not a ranking factor
When asked on X (previously Twitter) if there is a ranking advantage to using XML sitemaps, Google’s Gary Illyes responded:
- “No, not direct anyway.”
2. Google can find your pages without a sitemap
Websites don’t need a sitemap if they are small and linked properly, Daniel Waisberg, Search Advocate at Google, shared in a Google Search Console training video.
However, he followed up by saying a sitemap might be especially helpful if:
- The site is really large.
- The site’s pages are isolated.
- The site is new or changes quickly.

Also, it’s important to note that Google’s John Muller stated on X (previously Twitter) that having an XML sitemap is a “minimal baseline for any serious website.”