robots.txt and XML Sitemaps
Two files at the root of your site that decide what gets crawled and what gets found. Small, easy to write, and easy to break in ways that remove a whole site from search.
-
HTML Basics
- What is HTML: The Structure Layer of Every Web Page
- HTML Document Structure: DOCTYPE, html, head and body
- Elements, Tags and Attributes: The Vocabulary of HTML
- HTML Comments: Notes That Ship With Your Code
- Block Level and Inline Elements
- Writing and Running Your First HTML Page
- How a Browser Turns Markup Into a Page
- Text and Formatting
- Links and Navigation
- Images and Media
- Lists
- Tables
-
Forms
- Form Structure: form, action and method
- Input Types: Text, Email, Number, Date and the Rest
- Labels: The Most Important Element in a Form
- Checkboxes, Radio Buttons and Grouping
- select, option, optgroup and datalist
- textarea, File Uploads and Hidden Fields
- Buttons: submit, reset and button
- Built In Form Validation
- GET or POST: What Happens When a Form Is Submitted
- Semantic HTML
- HTML5 Features
- Head and Metadata
- HTML with CSS
- HTML with JavaScript
- Accessibility
-
HTML SEO
- How Google Works: Crawling, Indexing and Ranking
- SEO Friendly HTML Structure
- Titles and Descriptions That Earn Clicks
- Headings and Content Structure for Search
- Internal Linking and Anchor Text
- robots.txt and XML Sitemaps
- Canonical URLs and Duplicate Content
- Structured Data and JSON-LD
- Image SEO
- Core Web Vitals and Mobile Friendliness
- DevTools and Debugging
- Editor Productivity
- HTML Best Practices
- HTML Projects
- Advanced Projects
- Practice and Exams
robots.txt
A plain text file at the root of the site - https://example.edu/robots.txt - that tells crawlers which paths they may fetch.
User-agent: *
Disallow: /admin/
Disallow: /cart/
Disallow: /search
Allow: /
Sitemap: https://example.edu/sitemap.xml| Directive | Means |
|---|---|
User-agent | Which crawler the following rules apply to; * is all |
Disallow | Do not fetch paths starting with this |
Allow | An exception inside a disallowed path |
Sitemap | Where the sitemap lives; absolute URL |
What it does not do
This is the single most misunderstood point in technical SEO, so it is worth stating plainly.
robots.txtcontrols crawling, not indexing. A disallowed URL can still appear in search results.
If other pages link to a URL you have blocked, Google knows the URL exists. It cannot fetch the page, so it lists the URL with no description - often with a note saying no information is available.
Worse, because it never fetched the page, it never saw your noindex tag. Blocking a page you want removed from search actively prevents the removal.
| Goal | Correct approach |
|---|---|
| Keep it out of search results | Allow crawling, serve noindex |
| Save crawl budget on worthless URLs | Disallow in robots.txt |
| Keep it genuinely private | Authentication. Neither file is a security control. |
Rules and traps
- It must be at the root. A file at
/pages/robots.txtis ignored. - Paths are case sensitive.
- The most specific matching rule wins.
- Never block CSS or JavaScript. Google needs them to render the page; blocking them can make a responsive site look broken to the crawler.
- It is public. Anyone can read it, so listing your admin paths there advertises them.
# block everything - a staging leftover that removes a whole site
User-agent: *
Disallow: /That four line file, shipped to production, is one of the more expensive mistakes in web development. Check it at every launch.
XML sitemaps
A list of the URLs on your site that you want indexed, in a format crawlers read directly. It does not guarantee indexing, but it guarantees discovery.
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.edu/</loc>
<lastmod>2026-08-11</lastmod>
</url>
<url>
<loc>https://example.edu/courses/design</loc>
<lastmod>2026-07-30</lastmod>
</url>
</urlset>| Element | Required | Notes |
|---|---|---|
loc | Yes | Absolute URL, correctly escaped |
lastmod | No | Used if it is accurate; ignored if it always says today |
changefreq | No | Ignored by Google |
priority | No | Ignored by Google |
changefreq and priority appear in every sitemap tutorial and are ignored. Include loc, include lastmod if it is honest, and leave the rest out.
Limits and splitting
A sitemap may hold at most fifty thousand URLs and be at most fifty megabytes uncompressed. Beyond that, split it and add an index:
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>https://example.edu/sitemap-pages.xml</loc>
<lastmod>2026-08-11</lastmod>
</sitemap>
<sitemap>
<loc>https://example.edu/sitemap-articles.xml</loc>
</sitemap>
</sitemapindex>What belongs in it
Only URLs you want indexed:
- Return status 200, not a redirect or an error.
- Are not blocked in
robots.txt. - Do not carry
noindex. - Are canonical - the version you want indexed, not a duplicate.
A sitemap full of redirects and error pages is a signal that the site is not maintained, and Search Console reports every one of them.
Submitting it
- Reference it from
robots.txtwith aSitemap:line. - Submit it in Search Console, which then reports how many URLs were read and how many were indexed.
An image sitemap
<url>
<loc>https://example.edu/courses/design</loc>
<image:image>
<image:loc>https://example.edu/images/studio.jpg</image:loc>
</image:image>
</url>Useful when images are loaded lazily or by script and might not be discovered from the markup alone.
Generating them
Both files should be generated, not hand written, on any site with more than a few pages. On a database driven site the sitemap is a query and a template:
<?php
header("Content-Type: application/xml; charset=utf-8");
echo "<?xml version="1.0" encoding="UTF-8"?>
";
echo "<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
";
foreach ($pages as $page) {
printf(
" <url><loc>%s</loc><lastmod>%s</lastmod></url>
",
htmlspecialchars($page["url"], ENT_XML1),
$page["updated_at"]
);
}
echo "</urlset>
";A generated sitemap stays accurate. A hand written one is out of date within a month.
Important rules
robots.txtmust be at the root and is publicly readable.- Disallow blocks crawling, not indexing.
- A blocked page never has its
noindexread. - Never block CSS or JavaScript.
- Sitemap URLs must be absolute and canonical.
- Fifty thousand URLs per sitemap.
- Neither file is a security measure.
Common mistakes
- Shipping
Disallow: /from a staging environment. - Blocking a page in
robots.txtand expectingnoindexto remove it. - Blocking asset directories, so Google cannot render the page.
- Listing private paths in a public file.
- A sitemap containing redirects, 404s and non canonical URLs.
- Relative URLs in a sitemap.
- A
lastmodthat is always today. - Never submitting the sitemap.
Best practices
- Generate both files from the same source of truth as the site.
- Reference the sitemap from
robots.txtand submit it in Search Console. - Only include canonical, indexable, status 200 URLs.
- Use
lastmodhonestly or omit it. - Use
noindexfor exclusion androbots.txtfor crawl budget. - Check
robots.txtin production immediately after every deployment.
Practice
- Write a
robots.txtblocking an admin area, allowing everything else, and referencing a sitemap. - Explain in two sentences why blocking a page prevents it being removed from search.
- Generate a sitemap from a data source and validate the XML.
- Check whether your production
robots.txtblocks any CSS or JavaScript path.