Sub topic 13
Auditing slugs and internal links
This chapter assumes you know how slugs, 301 redirects, canonical tags, crawlable a elements, and anchor text work from the earlier chapters. Here you combine them into a repeatable check. An audit finds problems that individual good habits missed, such as a page edited years ago or a slug renamed without updating every link to it.
What to look for
Most audit findings fall into five groups.
| Problem | What it means | Usual fix |
|---|---|---|
| Orphan page | No internal link points to it | Link to it from a relevant hub or article |
| Broken link | The target returns 404 | Fix the href or restore the page |
| Redirected link | The link points at an old URL that returns a 301 | Change the href to the final URL |
| Redirect chain | A redirects to B, which redirects to C | Point A straight at C |
| Weak anchor | Text such as "click here" says nothing about the target | Rewrite it to describe the target page |
You should also check slugs themselves. Flag slugs with uppercase letters, underscores, tracking parameters, dates that will go stale, or more than about five words. Fix the slug only if the page has a real problem. Every slug change costs a redirect, so leave stable URLs alone.
The audit workflow
Get the list of known pages from your sitemap or CMS, not from the crawl alone. A crawl that follows links can never find an orphan, because by definition nothing links to it.
A small audit script
This script reuses the LinkCollector idea from the crawlable-markup chapter. It checks a tiny in-memory site for broken links, redirected links, redirect chains, orphans, and generic anchors. For a real site, replace the pages dictionary with fetched HTML.
from html.parser import HTMLParser
GENERIC = {"click here", "here", "read more", "learn more", "more", "this page"}
class LinkCollector(HTMLParser):
def __init__(self):
super().__init__()
self.links = []
self._href = None
self._text = []
def handle_starttag(self, tag, attrs):
if tag == "a":
self._href = dict(attrs).get("href")
self._text = []
def handle_data(self, data):
if self._href is not None:
self._text.append(data)
def handle_endtag(self, tag):
if tag == "a" and self._href is not None:
anchor = " ".join("".join(self._text).split())
self.links.append((self._href, anchor))
self._href = None
pages = {
"/": '<a href="/guides/url-slugs/">guide to URL slugs</a> '
'<a href="/blog/old-post/">click here</a> '
'<a href="/missing/">pricing details</a>',
"/guides/url-slugs/": '<a href="/">Home</a>',
"/blog/new-post/": "<p>No links.</p>",
"/guides/orphan/": "<p>Nothing links here.</p>",
}
redirects = {"/blog/old-post/": "/blog/new-post/"}
findings = []
inbound = {path: 0 for path in pages}
for old, new in redirects.items():
if new in redirects:
findings.append(f"redirect chain: {old} -> {new} -> {redirects[new]}")
for source, html in pages.items():
collector = LinkCollector()
collector.feed(html)
for href, anchor in collector.links:
if not href.startswith("/"):
continue # external link, outside this audit
if href in redirects:
findings.append(f"redirected link on {source}: {href} -> {redirects[href]}")
elif href not in pages:
findings.append(f"broken link on {source}: {href}")
if anchor.lower() in GENERIC:
findings.append(f"weak anchor on {source}: '{anchor}' -> {href}")
target = redirects.get(href, href)
if target in inbound and target != source:
inbound[target] += 1
for path, count in inbound.items():
if count == 0 and path != "/":
findings.append(f"orphan: {path}")
print("\n".join(findings))The script prints:
redirected link on /: /blog/old-post/ -> /blog/new-post/
weak anchor on /: 'click here' -> /blog/old-post/
broken link on /: /missing/
orphan: /guides/orphan/The page /blog/new-post/ is not reported as an orphan because the redirected link reaches it. It still needs the link updated, since each redirect adds a request and a place for the link to break later.
Prioritizing fixes
Fix problems in this order:
- Broken links on important pages, because they dead-end both readers and crawlers.
- Redirect chains and redirected links in navigation, because they sit on every page.
- Orphans that should rank. Some orphans, such as old campaign pages, should be removed or redirected instead of linked.
- Weak anchors, starting with the most-linked targets.
After you change a slug or an href, re-run the audit. Fixes often reveal new problems, such as a new orphan after a link is removed.
Exercise
An audit shows that /old-guide/ redirects to /guide/, which redirects to /guides/url-slugs/. Twelve internal links still point at /old-guide/. What should you change?
Show answer
Update the 301 for /old-guide/ so it points directly at /guides/url-slugs/, which removes the chain. Then edit the twelve internal links, and any links to /guide/, to use /guides/url-slugs/ so visitors and crawlers no longer pass through a redirect. Keep both redirects in place for external links and bookmarks.