XML Sitemaps and robots.txt
What each file does, how to write them correctly, and how to avoid the mistakes that hide pages from search engines or waste crawler visits.
Key takeaways
- robots.txt controls which URLs a compliant crawler fetches, and it does not reliably keep a page out of search results.
- To keep a page out of an index, let crawlers fetch it and serve a
noindexrule, or put it behind a login. - A sitemap should list only canonical, indexable URLs that return a successful response.
- A single sitemap file holds at most 50,000 URLs and 50 MB uncompressed, and larger sites use a sitemap index.
- Test changes before publishing them, because one stray slash can block an entire site.
A robots.txt file tells well-behaved crawlers which parts of your site they may request. An XML sitemap lists the pages you want them to find. One limits crawling and the other aids discovery, and neither one guarantees that a page will be indexed or ranked.
Most small sites need a short robots.txt and a sitemap that their publishing system generates automatically. The trouble usually starts when someone edits one of these files without knowing exactly what it controls.
How crawlers use the two files
Before a compliant crawler requests pages from a host, it fetches /robots.txt from the root of that host. It caches the rules and applies them to each URL it considers. Sitemaps come in separately: the crawler learns their location from a line in robots.txt or because you submitted them in a search engine's webmaster console.
Neither file replaces links. Crawlers still discover most pages by following them, and a page that appears only in a sitemap sends a weak signal about its importance. If you are new to this pipeline, how crawling, indexing and ranking fit together explains where each stage sits.
What robots.txt can and cannot do
The format is described in RFC 9309, the Robots Exclusion Protocol. It's a plain text file made of groups. Each group names one or more user agents and then lists Allow and Disallow rules.
User-agent: *
Disallow: /cart/
Disallow: /search
Allow: /search/help
Sitemap: https://www.example.com/sitemap.xml The rules are requests that reputable crawlers honor. They are not access control. Anyone can read your robots.txt, so listing a secret folder there advertises it. A scraper that ignores the protocol will fetch whatever the server hands out. Protect private material with authentication.
Blocking a URL also does not remove it from search. If other pages link to a blocked address, a search engine may still list the bare URL, often with no description, because it knows the page exists but was told not to read it.
How matching works
A few rules explain almost every surprise.
- A crawler follows the most specific group that names it. If there is a group for its own user agent, it uses that one and ignores the
*group. Rules are not inherited, so repeat any general blocks inside the specific group. - Paths match from the start.
Disallow: /searchblocks/search,/search-tipsand/search/results. Add a trailing slash if you mean only the folder. - The longest matching rule wins. In the example above,
/search/helpis allowed because theAllowpath is longer than theDisallowpath. -
*matches any run of characters and$marks the end of a URL.Disallow: /*.pdf$blocks addresses that end in.pdf. - Paths are case-sensitive.
/Private/and/private/are different.
The file also applies only to the exact protocol, host and port where it lives. shop.example.com needs its own file, separate from www.example.com. If you are deciding how to split a site, subdomains versus subdirectories covers what changes.
You can paste your rules and a test path into the robots.txt rule matcher to see which line applies before you deploy.
Server responses matter too
What your server returns for robots.txt changes crawler behavior. Under RFC 9309, if the file is missing (a 4xx status such as 404), a crawler may treat the site as having no restrictions. If the server errors (a 5xx status) or can't be reached, the crawler is expected to assume everything is disallowed, at least for a while.
That second case catches site owners off guard. A misconfigured firewall or an application that returns 503 for the robots file can pause crawling across the whole host. The status code families are worth knowing before you debug it.
Keeping a page out of the index
Use noindex when the goal is to stop a page appearing in results. You can set it in the page itself:
<meta name="robots" content="noindex"> For files that have no HTML, such as PDFs, send an HTTP response header instead:
X-Robots-Tag: noindex The crawler has to fetch the page to see either one. If robots.txt blocks the URL, the noindex instruction is never read. To retire an indexed page, remove the block, serve noindex (or a 404 or 410 status), and wait for it to be recrawled. Major search engines also offer temporary removal requests in their webmaster tools for urgent cases.
What goes in an XML sitemap
The sitemap protocol is simple. Each entry needs a loc with the full URL, and may include lastmod, the date the page last changed in a standard W3C date format.
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.example.com/guides/router-setup/</loc>
<lastmod>2026-11-02</lastmod>
</url>
</urlset> The protocol also defines changefreq and priority, but some major search engines say they ignore them, so there's little reason to tune them. lastmod can help, provided it is honest. If every page claims to have changed today because the site was rebuilt, crawlers have good reason to stop trusting the field.
Follow these rules for the list itself:
- Include only the canonical version of each page, the one address you want shown.
- Leave out redirects, error pages,
noindexpages and URLs blocked in robots.txt. - Use absolute URLs with the same protocol and host as the live site.
- Escape special characters, so
&in a query string becomes&. - Save the file as UTF-8.
Splitting and submitting sitemaps
When you pass 50,000 URLs or 50 MB uncompressed, split the list into several files and point to them from a sitemap index. Splitting earlier can be useful too. Separate files for products, articles and categories let you see in a webmaster console which group is being indexed poorly.
Sitemaps may be compressed with gzip. There are also extensions for images, video and news content, which matter if those vertical results drive your traffic.
Tell search engines where the files are in two ways: add a Sitemap: line with the full URL to robots.txt, and submit it in each engine's webmaster tools. Submission gives you error reports, which is the main reason to bother.
Mistakes that cost the most
The staging block that shipped. Development sites often use Disallow: /. If that file travels to production, crawlers stop fetching the whole site. Check robots.txt after every launch or migration.
Blocking style and script files. Search engines render pages much as a browser does. If they can't load the CSS and JavaScript, they may misread layout or miss content that scripts insert.
A sitemap that drifts. Hand-edited sitemaps go stale quickly. Generate them from your content database or publishing system so removed pages drop out on their own.
Using robots.txt to fix duplicates. Blocking a duplicate stops the crawler from seeing that it is a duplicate. A canonical link element or a redirect usually says what you mean, and redirecting pages properly keeps visitors and signals together.
Forgetting AI and other crawlers. Different companies publish different user agent names for search, AI training and other uses, and the names change. If you care about the distinction, read how sites can control AI crawlers and check each operator's own documentation.
A maintenance routine
Once the files are right, they need only light attention. Open /robots.txt in a browser after any deployment. Spot-check a handful of sitemap URLs and confirm they load without redirecting. Every so often, compare the number of URLs you submitted with the number reported as indexed. A large gap is not automatically a fault, but it tells you where to look, and the common reasons pages stay unindexed make a good checklist.
Common questions
Do I need a sitemap if my site is tiny?
A site with a few dozen well-linked pages will usually be crawled fine without one. A sitemap costs little if your software generates it, and the error reporting is handy.
Will a sitemap improve my rankings?
No. It helps crawlers find and revisit URLs. Whether a page ranks depends on its content, its usefulness and many other signals.
Should robots.txt block my site's internal search results?
Often yes. Internal search can create a nearly unlimited number of thin URLs. Block the path pattern, and make sure every page you care about is reachable through ordinary links.