Home / Blog / Special Characters in URLs Can Break Your Whole Sitemap
Indexing

Special Characters in URLs Can Break Your Whole Sitemap

23 September 2026 · AI Web Page SEO
Special characters in URLs break sitemaps because sitemaps are strict XML files. A raw ampersand makes the whole file invalid, so Google can reject every URL in it, and spaces create URLs that do not work. URL-encode each file and folder name, XML-escape every URL, validate the file, and prefer lowercase hyphenated names.

Sitemaps are XML files, and XML is strict. If a single URL in your sitemap contains a raw ampersand, the entire file becomes invalid, and Google can reject all of it, not just the one entry. File names with spaces cause a quieter problem: URLs that don't work. This guide explains how special characters in URLs break sitemaps and how to encode them properly.

Two different kinds of encoding

There are two separate jobs, and both are needed:

  1. URL encoding makes a file or folder name valid inside a URL. A space becomes %20, an ampersand becomes %26.
  2. XML escaping makes the whole URL valid inside the XML file. A literal & must be written as &amp;, and < as &lt;.

Many sitemap generators do neither. They take a file name and paste it straight into the URL, and the URL straight into the XML.

What goes wrong with spaces in file names

A file called my guide.html becomes https://example.com/my guide.html in the sitemap. Spaces aren't allowed in URLs, so crawlers may stop reading at the space or treat the address as invalid. The correct form is https://example.com/my%20guide.html, which your web server maps back to the real file.

Why one ampersand breaks the entire sitemap

XML treats & as the start of a special code, such as &amp;. A bare ampersand followed by an ordinary word is a syntax error. XML parsers stop at the first error, so a file named tips & tricks.html can make the whole sitemap unreadable, including hundreds of perfectly valid URLs.

We tested this with a small folder of pages. The old generator produced URLs with raw spaces and an invalid XML file. The fixed version produced tips%20%26%20tricks.html and a valid sitemap, while every normal URL came out exactly the same as before.

How to encode sitemap URLs correctly

Encode each part of the path separately

Encode each file and folder name on its own, not the whole URL, or the slashes get encoded too. In JavaScript, that means using encodeURIComponent() on each name.

Escape each URL for XML

After building the URL, escape it before writing it into <loc>: replace &, <, >, " and ' with their XML codes. Do this for every URL, including ones from your database, like blog and forum slugs.

Validate the result

Load the sitemap in a browser or run it through any XML validator. If it shows an error, the whole file is at risk.

The better fix: avoid special characters in URLs altogether

Encoding makes difficult names safe, but clean names are better for everyone. Use lowercase letters, numbers and hyphens: tips-and-tricks.html rather than Tips & Tricks.html. Clean URLs are easier to share, easier to read in search results and impossible to break.

Other sitemap checks worth doing

Summary

URL-encode each file and folder name, XML-escape every URL, and validate the finished file. Better still, use simple lowercase, hyphenated file names. To check that the pages in your sitemap load correctly, run them through the combined site audit.

Frequently asked questions

Why does one ampersand break an entire XML sitemap?

XML treats the ampersand as the start of a special code, such as the escaped form amp. A bare ampersand followed by an ordinary word is therefore a syntax error. XML parsers stop at the first error they find, so a single file named tips & tricks.html can make the whole sitemap unreadable, including hundreds of perfectly valid URLs. In a test on a small folder of pages, an old generator produced URLs with raw spaces and an invalid XML file, while the fixed version produced tips%20%26%20tricks.html and a valid sitemap, with every normal URL unchanged.

How should special characters in URLs be encoded for a sitemap?

URL encoding and XML escaping are two separate jobs, and sitemaps need both. URL encoding makes a file or folder name valid inside a URL, so a space becomes %20 and an ampersand becomes %26. It should be applied to each file and folder name separately, not the whole URL, or the slashes get encoded too; in JavaScript that means using encodeURIComponent on each name. XML escaping then makes the complete URL valid inside the XML file by replacing ampersands, angle brackets and quote marks with their XML codes before the URL is written into the loc element.

What other checks should a sitemap pass?

Beyond correct encoding, a sitemap should only list URLs that return a 200 status, because redirecting or missing pages waste crawl attention. Each page should be listed at its canonical URL, since an entry whose canonical tag points elsewhere is treated as a mistake. Pages marked noindex should be kept out, as there is no point asking Google to index a page you have told it not to index. The lastmod date should reflect when the page genuinely changed. Finally, load the sitemap in a browser or run it through an XML validator, because any error puts the whole file at risk.

💬 Got a problem?

aiwebpageseo.com is a data-driven SEO and AEO platform providing a free suite of technical website tools.