How to Create an XML Sitemap for SEO: Simple Guide
To create an
XML sitemap, write an XML file listing your website URLs inside <urlset> tags, each URL wrapped in <url> with child tags like <loc> for the link. This file helps search engines find and index your pages efficiently.Syntax
An XML sitemap uses the <urlset> root element with a namespace attribute. Each URL is inside a <url> tag containing:
<loc>: The full URL of the page.<lastmod>: Optional, last modified date in YYYY-MM-DD format.<changefreq>: Optional, how often the page changes (e.g., daily, weekly).<priority>: Optional, priority of the page from 0.0 to 1.0.
xml
<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>https://www.example.com/</loc> <lastmod>2024-06-01</lastmod> <changefreq>weekly</changefreq> <priority>1.0</priority> </url> </urlset>
Example
This example shows a complete XML sitemap with three URLs, including optional tags for last modification date, change frequency, and priority. It helps search engines understand your site structure and update frequency.
xml
<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>https://www.example.com/</loc> <lastmod>2024-06-01</lastmod> <changefreq>daily</changefreq> <priority>1.0</priority> </url> <url> <loc>https://www.example.com/about</loc> <lastmod>2024-05-20</lastmod> <changefreq>monthly</changefreq> <priority>0.8</priority> </url> <url> <loc>https://www.example.com/contact</loc> <priority>0.5</priority> </url> </urlset>
Output
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.example.com/</loc>
<lastmod>2024-06-01</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>https://www.example.com/about</loc>
<lastmod>2024-05-20</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://www.example.com/contact</loc>
<priority>0.5</priority>
</url>
</urlset>
Common Pitfalls
Common mistakes when creating XML sitemaps include:
- Using incorrect XML syntax or missing the XML declaration.
- Not including the required
xmlnsnamespace in<urlset>. - Using relative URLs instead of full absolute URLs in
<loc>. - Ignoring the sitemap size limit: max 50,000 URLs or 50MB uncompressed.
- Not updating
<lastmod>when pages change.
Always validate your sitemap with online tools or Google Search Console.
xml
<?xml version="1.0" encoding="UTF-8"?> <!-- Wrong: missing xmlns and relative URL --> <urlset> <url> <loc>/home</loc> </url> </urlset> <!-- Correct: includes xmlns and full URL --> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>https://www.example.com/home</loc> </url> </urlset>
Quick Reference
Tips for creating effective XML sitemaps:
- Always use full URLs starting with
https://. - Keep your sitemap updated with recent changes.
- Limit sitemap size to 50,000 URLs or split into multiple sitemaps.
- Submit your sitemap URL (e.g.,
https://www.example.com/sitemap.xml) to Google Search Console. - Use online validators to check for errors before publishing.
Key Takeaways
Create an XML sitemap using and tags with full URLs inside tags.
Include optional tags like , , and to help search engines.
Always use absolute URLs and include the required namespace in .
Keep your sitemap under 50,000 URLs or split it to multiple files.
Validate your sitemap and submit it to search engines for better indexing.