Prompt to find and fix broken external links
This prompt asks an AI assistant to collect every outbound link on your pages, check each one, and sort them into ok, redirected, broken and unverified. It fixes a link only when the new address is proven, and it rechecks anything that failed once, because many failures are one-time blocks.
Last reviewed
Key facts
- External links break slowly. Pages move, vendors rename help articles and old links keep answering through redirects.
- A 403 or 429 means the other site refused the check. It does not prove the link is broken.
- A redirect that lands on a different topic is worse than a 404. The prompt compares the old and new titles.
- Test status: the reference script was run on webexport.online (40 pages from its sitemap, 38 outbound links). One run reported 1 broken link, a 404 on a Squarespace help article, and a second run a minute later got a 200 with a redirect to a renamed article, so it was a one-time failure. It also found 5 links that redirect to a moved page and 1 that answered 403 and stayed unverified. Fixing the links with an assistant was not run.
The prompt
Paste this into Claude Code, Cursor, Codex or another AI coding assistant with your project folder open, and replace everything in square brackets first. See how to run it in each tool.
You are checking and repairing outbound links. The site is [LIVE_URL_OR_FOLDER]. Check up to [MAX_PAGES: 100] pages.
READ
- The sitemap at [LIVE_URL]/sitemap.xml, or every .html file in [EXPORT_FOLDER].
- Do not read pages outside that list.
STEP 1: COLLECT
List every link whose host is different from our own. Keep the pages it appears on. Ignore mailto, tel and javascript links.
STEP 2: CHECK
Request each unique URL with HEAD. If the answer is 400, 403, 405 or 501, repeat with GET. Follow redirects. Use a 12 second timeout and at most 6 requests at a time.
Sort each link into one class:
- ok: status 200 and the final address is the same as the link
- redirected: status 200 after a redirect to a different address
- broken: 404, 410, a server error or a DNS failure
- unverified: 401, 403, 429 or a timeout
Write external-links.csv with url, status, class, final_url and pages.
STEP 3: RECHECK
Recheck every broken and unverified link once, 60 seconds later. Update the class.
STEP 4: PROPOSE FIXES
For each redirected link, fetch the final page and compare its title with the old link text and with the sentence it sits in. If it is clearly the same topic, propose the final address. For each broken link, search the other site's own pages for the same topic. Propose a replacement only when you find one that matches.
Write LINK-FIXES.md with: page, link text, old URL, proposed URL, evidence. Wait for my approval before you edit anything.
RULES
- Never remove a link without telling me. Never replace a link with a page on a different topic.
- Do not edit any page except to change a URL I approved.
- Do not treat unverified as broken. List unverified links for me to open by hand.
- If scripts/extlinks.mjs exists, use it for steps 1 to 3.How to run it
- Run the prompt against the live site, or against a preview of the export.
- Open the unverified links in a browser yourself. A site that refuses a script often answers a person.
- Approve the fixes you agree with. For broken links with no clear replacement, remove the link or change the sentence yourself.
- Run the check again and keep the new CSV as the baseline for the next review.
Run it without a paid key
The script collects outbound links from sitemap pages, tries HEAD then GET, follows redirects and compares the final address with the link, ignoring the query string, because some sites add a language parameter to every redirect and would otherwise look moved. It does not recheck failures, so rerun it before you edit a link.
// Usage: node extlinks.mjs https://example.com 60 > external-links.csv
// Finds outbound links on sitemap pages and reports the ones that do not return 200.
const [base, limit = '60'] = process.argv.slice(2);
if (!base) { console.error('Usage: node extlinks.mjs https://example.com [limit]'); process.exit(1); }
const origin = new URL(base).origin;
const get = async (u) => { try { const r = await fetch(u, { signal: AbortSignal.timeout(15000), headers: { 'user-agent': 'extlinks/1.0' } }); return r.ok ? await r.text() : ''; } catch { return ''; } };
const locs = (x) => [...x.matchAll(/<loc>\s*([^<\s]+)\s*<\/loc>/g)].map((m) => m[1]);
let pages = locs(await get(`${origin}/sitemap.xml`));
if (pages.length && pages.every((u) => u.endsWith('.xml'))) pages = (await Promise.all(pages.map(async (u) => locs(await get(u))))).flat();
pages = [...new Set(pages)].filter((u) => u.startsWith(origin)).slice(0, Number(limit));
const found = new Map();
for (const p of pages) {
const html = await get(p);
for (const m of html.matchAll(/<a\s[^>]*href=["'](https?:\/\/[^"'#]+)["']/gi)) {
const u = m[1].replace(/&/g, '&');
if (new URL(u).origin === origin) continue;
if (!found.has(u)) found.set(u, new Set());
found.get(u).add(new URL(p).pathname);
}
}
async function check(u) {
for (const method of ['HEAD', 'GET']) {
try {
const r = await fetch(u, { method, redirect: 'follow', signal: AbortSignal.timeout(12000), headers: { 'user-agent': 'Mozilla/5.0 (compatible; extlinks/1.0)' } });
if (method === 'HEAD' && [400, 403, 405, 501].includes(r.status)) continue;
return { status: r.status, final: r.url };
} catch (e) { if (method === 'GET') return { status: 0, final: String(e.cause?.code ?? e.name) }; }
}
}
const bare = (u) => { try { const x = new URL(u); return x.origin + x.pathname.replace(/\/$/, ''); } catch { return u; } };
const klass = (s, u, f) => (s >= 200 && s < 300 ? (bare(f) === bare(u) ? 'ok' : 'redirected') : [401, 403, 429, 999].includes(s) ? 'unverified' : 'broken');
const list = [...found.keys()]; const out = [];
await Promise.all(Array.from({ length: 6 }, async () => { while (list.length) { const u = list.shift(); const r = await check(u); out.push({ u, ...r, c: klass(r.status, u, r.final) }); } }));
console.log('url,status,class,final_url,pages');
for (const o of out.sort((a, b) => a.c.localeCompare(b.c))) console.log([o.u, o.status, o.c, o.final, [...found.get(o.u)].join(' ')].map((v) => `"${v}"`).join(','));
const n = (c) => out.filter((o) => o.c === c).length;
console.error(`${pages.length} pages, ${out.length} external links: ${n('ok')} ok, ${n('redirected')} redirected, ${n('broken')} broken, ${n('unverified')} unverified`);What each class means
| Class | Typical status | What to do |
|---|---|---|
| ok | 200, same address | Nothing |
| redirected | 200 after one or more redirects | Update to the final address if it is the same topic |
| broken | 404, 410, 5xx, DNS failure | Recheck once, then replace or remove |
| unverified | 401, 403, 429, timeout | Open it in a browser and decide |
Firecrawl is not needed for this check. A scrape returns the page and its metadata, and its status code is in the response, but a plain HEAD request is cheaper and faster for a list of links. Use a scrape on the final page only when you need its title to judge a redirect.
Verify the result
Recheck the links you changed and confirm the file diff is only URLs.
node scripts/extlinks.mjs https://example.com 100 > external-links.csv
grep -c '"broken"' external-links.csv
git diff --stat
git diff -U0 | grep '^[+-]' | grep -v '^[+-][+-]' | grep -v 'https\?://' | head- The broken count is zero, or each remaining broken link is listed with a reason.
- The last command prints nothing, which means every changed line contains a URL.
- Each changed link opens the page you expect.
What can go wrong
- A one-time network failure looks like a dead link. The recheck step exists for this, and it caught one in our own run.
- Some sites answer HEAD differently from GET. The script falls back to GET for the common cases.
- A redirect to a home page or a search page can return 200 and still be a lost article. Read the final title.
Frequently asked questions
How often should I check external links?
Monthly is enough for most sites. Run it after a migration too, because exports can carry old links you never saw.
Is a redirected link a problem?
It still works, but each redirect is another hop and the target can change again. Updating to the final address is tidy, not urgent.
Why not fix every 404 automatically?
A replacement on a different topic misleads readers. The prompt proposes a fix only when it finds a page that matches and lists the evidence.
Is there a tool for internal links too?
Yes. The Broken Link Checker scans a sitemap or URL list and reports broken links with status codes.
Official documentation
Free tools for this job
- Broken Link CheckerScan sitemap or URL lists and report broken links with HTTP status insights.
- Sitemap URL ExtractorExtract all <loc> URLs from sitemap files, including sitemap index files.
- HTTP Header SEO InspectorInspect SEO-relevant HTTP headers for URL lists in bulk.
- Redirect Map BuilderBuild an old-to-final redirect mapping from URL lists.
Start with the files
Prompts work best on a WebExport download of your site. Paste your public URL into the export form and get a ZIP. The Free plan covers 1 site, 5 exports a month, 15 pages and 20 MB per export. Professional is a $9.99 one-time purchase with unlimited pages.