Sub topic 17
Keeping links healthy over time
This chapter assumes you can read status codes, trace redirects with curl, and run a small checker script like the one from the earlier chapters. Here you will turn those one-off checks into routines that stop broken links from coming back.
Why links break again
You can fix every broken link today and have new ones next month. Most new breakage comes from three events:
- Content is edited, renamed, or deleted, and nobody updates the pages that link to it.
- A site migration or redesign changes URL structures.
- External sites you link to move or disappear (link rot).
Prevention means catching each of these before or soon after it happens.
Build a regular check
Pick a schedule that matches how often your site changes. A small site that rarely changes can run a weekly check. A site that publishes daily should check daily.
The simplest setup is the checker script run from cron. This example checks a list of URLs and exits with a non-zero code when it finds a problem, so any scheduler or alerting tool can react to it.
import sys
import requests
def check(url):
try:
resp = requests.get(url, allow_redirects=True, timeout=10)
except requests.RequestException as exc:
return f"ERROR {url} ({exc.__class__.__name__})"
if resp.status_code >= 400:
return f"{resp.status_code} {url}"
if len(resp.history) > 1:
return f"CHAIN({len(resp.history)} hops) {url} -> {resp.url}"
return None
if __name__ == "__main__":
with open("urls.txt") as f:
urls = [line.strip() for line in f if line.strip()]
problems = [p for p in map(check, urls) if p]
print("\n".join(problems) or "all clear")
sys.exit(1 if problems else 0)Schedule it with cron:
0 6 * * 1 cd /opt/linkcheck && python3 check.py >> report.log 2>&1 || mail -s "Link check failed" [email protected] < report.logCheck before you publish
A check after the fact finds damage that is already live. Add the same script to your deployment pipeline so a build fails when key URLs return errors. Run it against a staging site first, using a list of your most important URLs: top landing pages, URLs from your redirect map, and the sitemap.
Habits that prevent breakage
- Keep your redirect map in version control, so every URL change has a recorded destination.
- When you delete a page, decide at that moment: redirect it, return 410, or update the links pointing to it.
- Never leave a redirect chain in place after a second move. Update the first redirect to point to the newest URL.
- Review the Search Console page indexing report and your 404 log report monthly. Compare the top 404 URLs with the previous month.
- Re-crawl your site after every migration or template change.
Summary
Healthy links come from routine: a scheduled check, a pre-deploy check, a redirect map under version control, and a monthly look at logs and Search Console.
Exercise
Your weekly link checker reports a chain of three hops for /blog/old-post. What do you change, and why?
Show answer
Update the first redirect so /blog/old-post points straight to the final URL in one hop, and update internal links to use the final URL. Fewer hops mean faster loads, less wasted crawling, and fewer places for the chain to break later.