Sub topic 12
Publishing an XML sitemap
This chapter assumes you have finished the planning sitemap and chose your URL slugs in earlier chapters. You should also know what a URL and a canonical page address are. The goal here is to turn your final list of pages into a file that search engines can read.
Two kinds of sitemap
The word "sitemap" covers two different things. Keeping them apart avoids a common mix-up.
| Row | Planning sitemap | XML sitemap |
|---|---|---|
| Audience | You, designers, developers, clients | Search engine crawlers |
| Format | Diagram or indented tree | XML file |
| Purpose | Decide structure and hierarchy | List the URLs you want indexed |
| When made | Before design | After the site is built and URLs are final |
The planning sitemap shows hierarchy and relationships. The XML sitemap is a flat list of addresses. It does not show parents, children, or navigation.
What the file looks like
The planning sitemap from earlier in this tutorial has five main pages plus two service pages. Here is an XML sitemap for the first three URLs, using the placeholder domain example.com.
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.example.com/</loc>
</url>
<url>
<loc>https://www.example.com/services/</loc>
</url>
<url>
<loc>https://www.example.com/contact/</loc>
</url>
</urlset>The structure has three parts:
urlsetis the root element. Itsxmlnsattribute declares the sitemap protocol, and you should copy it exactly.- Each
urlelement describes one page. - Each
locelement holds the full address of the page. It is the only required child ofurl.
Optional elements such as lastmod exist. Only include lastmod if you can keep it accurate. A date that changes on every build, or never changes, teaches search engines to ignore it.
Rules for the URLs you list
The loc values must follow a few rules.
- Use absolute URLs. Write
https://www.example.com/services/, not/services/. - Use the canonical form. Pick one scheme (
https), one host (with or withoutwww), and one trailing-slash style. Use the same choices in every entry, and make sure they match your slugs from the naming chapter. - List only pages you want in search results. Leave out redirects, error pages, thank-you pages, and pages blocked from indexing.
- List pages that return a normal success response. A URL that returns a 404 error or redirects elsewhere wastes crawler time.
A mismatch is the most common error. If your site serves https://example.com/services/ but the sitemap lists http://www.example.com/services, crawlers see conflicting signals about which version is canonical.
Publishing and submitting
Follow these steps once the site is live.
- Save the file as
sitemap.xmlin the root of the site, so it is reachable athttps://www.example.com/sitemap.xml. - Open that address in a browser and confirm the XML displays without errors.
- Add a line to your
robots.txtfile so crawlers can discover it:
Sitemap: https://www.example.com/sitemap.xml- Submit the address in the webmaster tools of the search engines you care about, such as Google Search Console or Bing Webmaster Tools. These tools also report which listed URLs were read and which had problems.
A site builder or CMS often generates the file for you. In that case, check the output against your planning sitemap. Confirm that every page you planned appears, and that no draft or utility pages were added.
Size limits and maintenance
A single sitemap file can hold up to 50,000 URLs and must be no larger than 50 MB uncompressed. A small site will not come close. If you ever exceed the limits, split the list across several files and reference them from a sitemap index file.
Update the file whenever you add, remove, or rename a page. If you change a slug, update the sitemap and add a redirect from the old URL.
Common mistakes
- Listing URLs that differ from the canonical ones in scheme, host, or trailing slash.
- Including pages that redirect or return errors.
- Forgetting to update the file after a restructure.
- Treating the XML sitemap as a ranking tool. It helps crawlers find pages. It does not make pages rank higher.
- Using the XML file as a replacement for good navigation. Visitors and crawlers still need links between pages.
Exercise
Your site uses https://www.example.com with a trailing slash on every page. A teammate's sitemap.xml contains <loc>/portfolio</loc> and <loc>http://example.com/about/</loc>. Name the problem with each entry and write the corrected value.
Show answer
The first entry is a relative URL, and the file requires absolute URLs. It also lacks the trailing slash used by the site. The correct value is https://www.example.com/portfolio/. The second entry uses the wrong scheme (http) and the wrong host (no www), so it does not match the canonical form. The correct value is https://www.example.com/about/.
Exercise
Which of these pages belong in the XML sitemap for the small business site: /services/, /contact/thank-you/ (shown only after a form is sent), /old-services/ (redirects to /services/), /portfolio/?
Show answer
Include /services/ and /portfolio/. Leave out /contact/thank-you/ because it is a utility page that should not appear in search results. Leave out /old-services/ because it redirects, and the sitemap should list only the final destination.