Sub topic 9
Crawling your site to find broken links
A crawler follows links the way a search engine bot does. It starts at one URL, fetches the page, extracts every link, and repeats for each link it hasn't seen yet. Along the way it records each URL's status code and the pages that link to it. This chapter assumes you can read status codes (200, 301, 404, 410, 500) and know how redirects work. Both were covered in earlier chapters.
What a crawler gives you that curl does not
curl tells you about one URL you already suspect. A crawler finds the URLs you didn't know to check. It also records the source page for each bad link, and that is the information you need to fix it.
A typical crawl report has these fields:
| Field | Why it matters |
|---|---|
| Destination URL | The URL that failed or redirected |
| Status code | 404, 410, 301, 500, and so on |
| Source page | The page that contains the link, which is where the fix goes |
| Anchor text or element | Helps you find the link in the HTML or CMS |
| Redirect target and hop count | Shows chains that should be collapsed |
| Crawl depth | Clicks from the start page, a rough measure of importance |
How the crawl loop works
Three details of this loop explain most surprises in crawl results:
- Scope decides what is followed. Internal URLs are fetched and parsed for more links. External URLs are usually fetched once to check their status, but their links are not followed.
- The crawler only finds what is linked. A page with no inbound links (an orphan) is invisible unless you also seed the crawl from your XML sitemap.
- Each URL is recorded once. The same broken URL linked from 400 pages is one row with 400 sources, not 400 rows. Check that your tool shows all the sources.
Choosing a tool
- Desktop crawlers such as Screaming Frog SEO Spider or Sitebulb give a UI, filtered reports, and JavaScript rendering. The free tier of Screaming Frog limits the number of URLs per crawl.
- Command-line checkers such as LinkChecker are scriptable and fit into CI or cron.
- Hosted monitors run scheduled crawls and send alerts. These suit teams that don't want to maintain tooling.
A command-line crawl with LinkChecker looks like this:
linkchecker --recursion-level=5 --check-extern --no-warnings \
--threads=5 --output=csv --file-output=csv/linkchecker.csv \
https://example.com/--recursion-level limits depth. --check-extern also tests external links. --threads sets how many requests run at once. Keep this low on small servers.
Configuring a crawl
Default settings are rarely right for a real site. Check these before you start.
- Start URL and scheme. Start from the canonical form (
https://, with or withoutwwwto match your site). Starting from a redirecting variant adds noise. - Seed with the sitemap. This finds orphan pages. It also lets you compare the sitemap against the crawl. A URL in the sitemap that returns 404 or 301 is a bug in the sitemap itself.
- Respect or override robots.txt deliberately. Your own site's crawl should usually obey robots.txt, because that shows what search engines can reach. Run a second crawl that ignores it if you want to audit blocked sections.
- Rate limit. Start at 2 to 5 concurrent requests. A fast crawl can slow a shared server or trigger a rate limiter, which then returns 429 or 503 and pollutes the report.
- User agent. Some firewalls block unknown crawlers. If your firewall allows it, use a recognizable agent string or allowlist your crawler's IP.
- JavaScript rendering. If links are injected by JavaScript, a plain HTML crawl will miss them. Rendered crawls are slower, so turn them on only when the site needs them.
- Authentication and parameters. Logged-in areas need credentials. Faceted navigation (
?color=red&size=m) can generate millions of URLs, so exclude or limit parameters.
Reading the results
Work through the report in this order.
1. Internal 4xx errors
These are the highest priority because you control both ends. Open the inlinks (source pages) for each URL. Most are one of three causes: a typo in the link, a page that was deleted or renamed, or a link generated by a template with a wrong path. Fixing one template can clear hundreds of rows.
2. Internal redirects
A 301 on an internal link works, but it adds a hop and wastes crawl budget. The report's redirect section shows the final destination. Update the link to point there directly. Chains of two or more hops come first.
3. 5xx errors
Recrawl these before acting. A 500 or 503 that disappears on the second request was probably load or a deploy. One that persists is an application bug, and it needs fixing at the application level instead of in your content.
4. External broken links
These are lower priority than internal ones but still harm users. You can only change your own link: update it, replace it, or remove it.
Separating real errors from false positives
External checks produce noise. Treat these statuses with suspicion before editing anything:
| Result | Likely meaning | What to do |
|---|---|---|
| 403 on external site | The site blocks bots | Open the link in a browser |
| 429 | You are rate limited | Lower the speed and retry later |
| 405 or 501 | The server rejects HEAD requests | Recheck with GET |
| Timeout | Slow or temporarily down | Retry on another day |
| 200 with "not found" text | A soft 404 | Inspect the page, not the code |
The last row matters because a crawler reads status codes, not page text. Soft 404s are covered in their own chapter, but you can often catch them in a crawl by sorting on very short pages or pages sharing the same title.
Post-processing an export
Crawler exports are usually CSV. This script assumes columns named source, destination, and status, so rename columns to match if your tool uses different headers. It groups the broken destinations by how many pages link to them:
import csv
import sys
from collections import defaultdict
BROKEN = {"404", "410"}
def main(path):
sources = defaultdict(set)
with open(path, newline="", encoding="utf-8") as f:
for row in csv.DictReader(f):
if row["status"] in BROKEN:
sources[row["destination"]].add(row["source"])
ranked = sorted(sources.items(), key=lambda item: len(item[1]), reverse=True)
for destination, pages in ranked[:20]:
print(f"{len(pages):4d} pages -> {destination}")
for page in sorted(pages)[:3]:
print(f" e.g. {page}")
if __name__ == "__main__":
main(sys.argv[1])Run it as python broken_by_inlinks.py crawl.csv. A destination with many inlinks is usually a template or navigation problem, so fix those first.
Limits of crawling
- A crawl is a snapshot. It cannot see links that appear only after a user action, such as clicking a tab or submitting a form.
- It sees your site as one client with one user agent. Content served differently to Googlebot or to logged-in users can differ from what you crawled.
- It cannot tell you which broken URLs people or search engines actually request. For that, use server logs and Search Console, which the next chapter covers.
Exercise
A crawl reports 150 broken internal links, all with destination /products/old-widget, and they come from 150 different pages. External links to a third-party site return 403 in the same report. What do you fix first, and what do you do about the 403s?
Show answer
Fix /products/old-widget first. With 150 sources it is most likely a shared template, navigation, or related-products block, so one fix removes all 150 rows. Update the link to the current product page, or add a one-hop 301 if the product moved. If the product is gone permanently, remove the link and return 410. For the 403s, open the URLs in a browser and recheck with GET. A 403 to a crawler often means the site blocks bots, not that the page is broken. Change the link only if it fails for real users too.
Exercise
Why should you seed a crawl with your XML sitemap as well as the home page?
Show answer
A crawler only finds pages that are linked from pages it has already visited. Orphan pages have no inbound links, so a crawl from the home page never reaches them. Seeding with the sitemap adds those URLs. It also lets you detect sitemap entries that return 404 or redirect.