Sub topic 4
Broken Link Building from Start to Finish
This chapter assumes you know what a backlink and anchor text are, can read basic HTML anchor tags, and recognize HTTP status codes such as 200, 301 and 404. It walks through the full broken link building workflow once, so later chapters can go deeper on each step.
The idea in one paragraph
Many pages link out to resources that no longer exist. A visitor who clicks such a link lands on an error page. Broken link building means finding those dead outbound links on relevant sites. You then offer the site owner a working replacement, ideally a page of yours that covers the same ground. The owner fixes a problem, and you earn an editorially placed link.
The workflow
- Find relevant pages. Look for resource pages, link roundups and guides in your topic. Search operators help, for example
intitle:resources "web analytics" inurl:links. - Detect broken links. Check each outbound link on those pages and keep the ones that fail.
- Check what the dead page covered. A web archive snapshot shows what the missing page said, so your replacement can match it.
- Choose or create a replacement. Use an existing page of yours if it truly fits. Otherwise write one.
- Send outreach. Tell the owner about the dead link and offer your page as a fix.
- Track results. Record who replied, who added the link, and what share of attempts succeeded.
Detecting broken links
A link counts as broken when it fails to connect or returns a 404, 410 or server error (5xx). The script below checks every link on one page. It sends a HEAD request first, which asks for headers only. Some servers reject HEAD with 403, 405 or 501, so the script retries those with GET.
import requests
from urllib.parse import urljoin, urlparse
from bs4 import BeautifulSoup
HEADERS = {"User-Agent": "link-audit/1.0"}
def find_broken_links(page_url, timeout=10):
html = requests.get(page_url, headers=HEADERS, timeout=timeout).text
soup = BeautifulSoup(html, "html.parser")
broken = []
seen = set()
for a in soup.find_all("a", href=True):
url = urljoin(page_url, a["href"])
if urlparse(url).scheme not in ("http", "https") or url in seen:
continue
seen.add(url)
try:
resp = requests.head(url, headers=HEADERS, allow_redirects=True, timeout=timeout)
if resp.status_code in (403, 405, 501):
resp = requests.get(url, headers=HEADERS, stream=True, timeout=timeout)
resp.close()
status = resp.status_code
except requests.RequestException:
status = None
if status is None or status in (404, 410) or status >= 500:
broken.append((url, status))
return broken
if __name__ == "__main__":
for url, status in find_broken_links("https://example.com/resources"):
print(status, url)A status of None means the connection failed, for example because the domain no longer exists. A 5xx error can be temporary, so recheck those links on another day before you write to anyone.
Writing the outreach
Name the exact broken link, offer one specific replacement, and leave the decision to the owner.
Subject: Quick fix for a dead link on your resources page
Hi Sam,
While reading your page on web analytics basics, I noticed the link to "Guide to Event Tracking" returns a 404 error.
We published a guide on the same topic that covers the same ground and is kept up to date: https://example.com/event-tracking-guide
If it fits, it could replace the broken link. Either way, I thought you would want to know about the error.
Thanks,
AlexCommon errors
- Reporting links that work. Redirects can hide a healthy page. The script follows redirects, so it only flags final failures.
- Pitching a weak match. If your page does not cover what the dead page did, the owner has no reason to swap it in.
- Mass-mailing a template. Each message should mention the specific page and link.
Exercise
Your script reports a link with status 503 on a resources page. What should you do before contacting the site owner?
Show answer
Recheck the link later, ideally on another day, because a 503 can be a temporary outage. Open it in a browser too. Only report it if it keeps failing.