Sub topic 13
Fixing the link, the redirect, or neither
This chapter covers what to do once you have found a broken URL. It assumes you can read status codes (200, 301, 404, 410), know the difference between permanent and temporary redirects, and can trace a URL with curl. It also assumes you have a prioritized list of problems from a crawler, your server logs, or Search Console.
Not every broken URL needs a redirect. Each one needs a decision. The wrong fix wastes effort or makes things worse, so the first step is to work out which fix applies.
The three fixes and the fourth option
There are three ways to repair a problem URL, and one case where you do nothing.
- Fix the link. Edit the page that contains the bad link so it points at the right URL.
- Add a redirect. Make the old URL answer with a 301 (or 308) to the new location, so old links, bookmarks, and search results still arrive.
- Return 410. Tell clients the content is gone on purpose.
- Leave it as a 404. Some URLs should stay broken.
The choice depends on two questions: who controls the link, and does the content still exist somewhere?
Fix the link first
If the broken link sits on a page you control, edit it. This is the cheapest and most reliable fix. A corrected link costs no extra request, passes no redirect penalty, and does not depend on server configuration that someone may later delete.
Typical internal causes:
- A typo in the path (
/prodcuts/instead of/products/). - A page that was renamed or moved, with links still using the old path.
- A link written as an absolute URL to an old hostname, such as
http://instead ofhttps://, or a retired staging domain. - A link in a template, menu, or footer. These repeat on every page, so one fix removes hundreds of errors.
Fix the source in the place where it is stored: the CMS, the template, or the database. Editing the rendered HTML will not last. After a bulk change, search the codebase or content export for the old path to confirm no copies remain.
grep -rn --include='*.html' --include='*.md' '/old-page' ./contentFixing the link also helps when a redirect already exists. A link that points to /old-page, which redirects to /new-page, works but costs an extra request on every click and every crawl. Update the link to /new-page. This is how you collapse a redirect chain from the inside.
Add a redirect when the old URL still has value
You cannot edit links on other people's sites, in old emails, in printed material, or in a user's bookmarks. If the old URL receives visits or has external links pointing at it, a redirect keeps that value.
Use a redirect when:
- The content moved to a new URL, or was merged into another page.
- The old URL has backlinks from other sites.
- Logs or Search Console show real requests for it.
Follow these rules:
- Use a permanent redirect. 301 is the usual choice. 308 is the same, but it also forbids changing the request method, which matters for POST requests and APIs. Use 302 or 307 only if the move is really temporary.
- Point to the final URL. Never redirect to a URL that itself redirects. Look up the destination's current address first, then write a single hop.
- Match the content. Send
/old-guide-to-widgetsto the closest equivalent page, not to the homepage. A redirect to an unrelated page is treated by search engines much like a soft 404, and it confuses users. - Keep it a one-to-one mapping where you can. Mass redirects to a single catch-all page lose the specific relevance of each old page.
Here is the nginx form. The exact-match = prevents the rule from catching other paths.
# Redirect an old URL straight to its final destination (one hop)
location = /old-page {
return 301 /new-page;
}Apache's mod_alias does the same job:
# Requires mod_alias
Redirect 301 /old-page https://example.com/new-pageVerify every redirect you add. Do not assume the config reload worked.
curl -sL -o /dev/null -w '%{num_redirects} redirects, final status %{http_code}, final URL %{url_effective}\n' https://example.com/old-pageYou want 1 redirects, final status 200 and the URL you intended. If you see 2 or more, you have a chain. If the status is not 200, the destination is itself broken.
Collapsing existing chains
Chains usually grow over time. A page moves in 2021, again in 2023, and each move adds a redirect to the previous destination. To fix this:
- Trace the chain and note the first and last URL.
- Change the first redirect to point at the final URL.
- Update internal links to the final URL.
- Leave the intermediate redirects in place if anything still requests them, but they should no longer sit in the middle of a path.
curl -sIL https://example.com/old-page | grep -iE '^(HTTP|location)'For a loop, the cause is two rules that point at each other, or a rule that matches its own destination. Remove or narrow one rule. Test again until the trace ends in a 200.
Return 410 when content is gone on purpose
If you deleted a page deliberately and there is no replacement, 410 Gone is more precise than 404. It says the removal is permanent and intended. Search engines tend to drop a 410 URL from their index somewhat faster, though in practice both codes are handled similarly, so do not expect a dramatic difference.
Examples: a discontinued product with no successor, an expired campaign, spam pages from a hack that you cleaned up.
location = /discontinued-widget {
return 410;
}Redirect gone /discontinued-widgetDo not return 410 for URLs that might come back. Clients may cache the signal, and a recovery takes longer.
When to do nothing
Leaving a 404 is correct more often than people expect. A 404 is the right answer for a URL that never existed or has no meaningful replacement.
- Random junk requests. Bots probe
/wp-login.phpor/.env. These show up in logs. Ignore them. - Typos in external links with no traffic. If no one visits the URL, a redirect adds maintenance cost for nothing.
- Old pages with no value and no replacement. Redirecting them to the homepage only hides the problem.
The cost of an unneeded redirect is real: the rule list grows, and every rule must be tested on each migration. Add redirects for URLs that earn them.
Comparing the options
| Situation | Best fix | Why |
|---|---|---|
| Typo in your own page's link | Edit the link | Cheapest, no extra request |
| Page moved, many internal links | Edit links, add 301 | Clean internally, preserves external value |
| Page moved, backlinks from other sites | 301 to final URL | You cannot edit their links |
| Content merged into another page | 301 to the merged page | Keeps relevance |
| Content removed on purpose, no successor | 410 | Clear permanent signal |
| Bot probes and junk URLs | Nothing | A 404 is correct |
| Broken link on a third-party site you link to | Replace or remove the link | You cannot fix their site |
Fixing outbound links
When your page links to someone else's page that now returns 404, you have three options:
- Replace it with a working equivalent page.
- Link to an archived copy, if the content is still useful to readers.
- Remove the link and keep the text.
Do not add a redirect for this. The broken URL is on a domain you do not control.
Check the result
After each batch of fixes, run the same check that found the problem. Re-crawl the affected section, or run the script from the earlier chapter on your list. Confirm three things: the original error is gone, no new chain was created, and the final status is 200. Then watch Search Console over the following weeks, since its reports update slowly.
Use this small script to confirm that a fixed URL ends cleanly. It follows redirects hop by hop and flags loops and long chains.
import requests
from urllib.parse import urljoin
REDIRECT_CODES = {301, 302, 303, 307, 308}
def trace(url, max_hops=10):
hops = []
seen = {url}
while len(hops) < max_hops:
resp = requests.get(url, allow_redirects=False, timeout=10, stream=True)
resp.close()
hops.append((resp.status_code, url))
location = resp.headers.get("Location")
if resp.status_code not in REDIRECT_CODES or not location:
return hops, "ok"
url = urljoin(url, location)
if url in seen:
hops.append((None, url))
return hops, "loop"
seen.add(url)
return hops, "too_many_hops"
if __name__ == "__main__":
hops, outcome = trace("https://example.com/old-page")
for status, hop_url in hops:
print(status, hop_url)
print("outcome:", outcome)A good result is a single 301 line followed by a 200 line and outcome: ok.
Common mistakes
- Redirecting to a URL that redirects again, which creates a chain.
- Using 302 for a permanent move, so search engines keep the old URL indexed longer.
- Redirecting to the homepage or an unrelated page to "clear" the error.
- Forgetting query strings or trailing slashes.
/old-pageand/old-page/are different URLs, and a rule may match only one. - Adding a redirect without testing it, then finding it loops with an existing HTTP to HTTPS rule.
- Fixing the symptom in rendered HTML while the template still produces the bad link.
Exercise
A product page at /shop/blue-widget was renamed to /shop/widgets/blue. Three internal pages link to the old URL, two other websites link to it, and the old URL still gets 40 visits a week. Which fixes do you apply, and why? Then, a month later, a trace of the old URL shows two 301 hops ending at /shop/widgets/blue. What went wrong, and what do you change?
Show answer
Update the three internal links to /shop/widgets/blue, because you control them and this removes an extra request. Add a single 301 from /shop/blue-widget to /shop/widgets/blue, because you cannot edit the two external links and the URL still has visitors. The two-hop trace means the old URL redirects to an intermediate URL that then redirects to the final one, for example a trailing-slash or HTTP to HTTPS rule applied first, or an earlier rename. Change the original redirect so it points straight at the final URL (https, correct slash, final path), then trace again and expect one redirect and a 200.
Exercise
Your logs show thousands of 404s for /wp-login.php and /.env, and a handful for /blog/2019-recap, a post you deleted on purpose with no replacement. What do you do for each?
Show answer
Do nothing for /wp-login.php and /.env. They are bot probes, and a 404 is the correct response. For /blog/2019-recap, return 410 Gone if you want to signal the removal is deliberate and permanent. Leaving a 404 is also acceptable. Do not redirect either to the homepage.