XML sitemaps and SEO: What you need to know

XML sitemaps and SEO: What you need to know

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

When working on large websites at an enterprise level, there are often multiple layers of decision-makers on a website navigation structure. And oftentimes, SEO best practices are taken into consideration. 

I pair both my XML sitemap with an HTML sitemap until I’m confident in our website navigation and internal linking hierarchy. 

15. Don’t forget to include all your XML sitemaps in your robots.txt file

Don’t forget to include your XML sitemaps (all of them) as links in your robots.txt file. 

Here is an example: 

Include all your XML sitemaps in your robots.txt file

Don’t worry about where in the robots.txt file these should go. There is no impact on the location of your XML sitemap in your robots.txt file

Mueller confirmed this: 

XML sitemaps are the blueprint of your website for search engines

Whether you’re launching a new webpage or just adding a canonical tag to a duplicate page, your XML sitemap is something you should update 24/7.

But do so with caution. 

While you shouldn’t be paranoid, your XML sitemap is the blueprint of your house for search engines. 

So don’t forget to submit your XML sitemaps to Google Search Console and Bing Webmaster Tools.

Opinions expressed in this article are those of the guest author and not necessarily Search Engine Land. Staff authors are listed here.

Read More

Leave a Reply

Your email address will not be published. Required fields are marked *