Sub topic 15
Testing and troubleshooting both files
This chapter assumes you can read a robots.txt file and a basic sitemap, and that you know HTTP status codes. It shows how to check both files and how to fix the most common failures.
Test the files in a fixed order
Problems in one file often look like problems in the other. Work through the checks in this order.
Testing robots.txt
First, request the file in a browser or with curl -i https://www.example.com/robots.txt. It must be at the host root. Crawlers treat the status code as an instruction:
| Response | How crawlers treat it |
|---|---|
| 200 | Rules are read and applied |
| 404 | No restrictions, everything may be crawled |
| 5xx or timeout | Crawling may pause for the whole site until the file loads |
Second, test specific URLs against the rules. Python's built-in parser works for simple prefix rules:
from urllib.robotparser import RobotFileParser
rp = RobotFileParser()
rp.parse(["User-agent: *", "Disallow: /private/"])
print(rp.can_fetch("*", "https://www.example.com/private/report.html"))
print(rp.can_fetch("*", "https://www.example.com/blog/"))This prints False, then True. The standard-library parser does not understand * or $ inside paths, and it uses the first matching rule. It can therefore disagree with Google's longest-match behavior. To test wildcard rules, use the robots.txt report and URL Inspection tool in Google Search Console.
Testing the sitemap
Check three things:
- The XML is well formed and uses the namespace
http://www.sitemaps.org/schemas/sitemap/0.9. - Every
locis an absolute URL that returns a 200 status without redirecting. - No listed URL is blocked by robots.txt or marked
noindex.
This script covers the second check:
import urllib.request
import xml.etree.ElementTree as ET
NS = {"s": "http://www.sitemaps.org/schemas/sitemap/0.9"}
def check_sitemap(path):
root = ET.parse(path).getroot()
for loc in root.findall("s:url/s:loc", NS):
url = loc.text.strip()
req = urllib.request.Request(url, method="HEAD")
try:
with urllib.request.urlopen(req, timeout=10) as resp:
moved = resp.geturl() != url
print(resp.status, "REDIRECTED" if moved else "ok", url)
except Exception as err:
print("ERROR", url, err)
check_sitemap("sitemap.xml")Common problems and fixes
- "Indexed, though blocked by robots.txt." A
Disallowrule stops crawling, but the URL can still be indexed if other pages link to it. To remove it, allow crawling and addnoindex. A crawler that cannot fetch the page never sees the tag. - A sitemap URL is blocked. Sending crawlers a URL and forbidding them to fetch it is a contradiction. Remove either the rule or the sitemap entry.
- A rule does not match. Paths are case-sensitive and match from the start of the path.
Disallow: /adminalso blocks/administrator. Add the trailing slash to limit the match. - An
Allowrule loses. The most specific (longest) matching rule wins. IfDisallowandAlloware equally specific,Allowwins. - Sitemap errors. Common causes are unescaped
&in a URL (it must be&), a relative URL, a wrong host or protocol (httpversushttps), and more than 50,000 URLs or 50 MB uncompressed in one file. - Stale
lastmod. If every page shows today's date, crawlers learn to ignore the field. Update it only when the content changes.
Check Search Console
Submit the sitemap in Search Console and read its status. "Couldn't fetch" usually means the URL returns a non-200 status or is blocked. A large gap between submitted and indexed URLs points at page quality, duplicates, or noindex. It does not point at the sitemap.
Exercise
A page is listed in your sitemap, and Search Console reports it as "Indexed, though blocked by robots.txt". You want it out of search results. What two changes do you make, and why in that order?
Show answer
First remove the Disallow rule that covers the page, so crawlers can fetch it. Then add a noindex robots meta tag or X-Robots-Tag header to the page, and remove the page from the sitemap. Crawlers can only see noindex if they are allowed to fetch the page. Once it drops out of the index, you can block it again if you need to.
Exercise
Your sitemap check script prints "REDIRECTED" for many URLs. What should you put in the sitemap instead?
Show answer
Put the final destination URL, the one that returns 200, in each loc entry.