Exported HTML Moved Domains and Images Broke? Check Links
The baseline for urljoin should be the original page URL; do not arbitrarily append a trailing slash to convert a file URL into a directory. This example handle
Why do images break after a domain move? The short answer
Keep the original page URL as the base for urljoin, normalise base URLs to directories before joining, and check rendered results in a real browser.
| Your situation | What to do | Why |
|---|---|---|
| Exported HTML is moved to a new domain | Use the original page URL as the baseline for urljoin | Relative paths are resolved against the base, so the base must match where the file originally lived |
| The base URL is a directory but lacks a trailing slash | Normalise it by appending / before joining | urljoin otherwise treats the last segment as a file name and drops it |
| Links are already absolute paths or full URLs | Let urljoin return them or replace only the hostname | This is safer than string concatenation |
Links are generated by JavaScript, sit in Shadow DOM, or appear in CSS background-image or Canvas/WebGL textures | Test in a real browser, or use Playwright or Puppeteer for end-to-end visual regression testing | BeautifulSoup cannot reach these references |
The page contains fragment anchors (#section) | Keep them pointing to the current page | They do not need to be changed to a remote page |
Who this guide is for, and what you will get
If you are moving internal documents, wikis or legacy sites to a new domain, you may find that images disappear and links point to the wrong place. This guide explains why that happens and how to rewrite resource links in exported static HTML so they keep working.
The problem is rarely lost files. It is usually lost parsing context: the relative paths no longer resolve against the directory they were written for. Once you understand how the browser resolves those paths, the fix becomes a matter of setting the right baseline.
You will learn how the browser resolves relative paths, where urllib.parse.urljoin differs from the HTML <base> tag, how to handle edge cases, and what automated scripts cannot verify on their own.
Note: The code in this guide is a synthetic example. Keep the original copies of important files and verify results against the acceptance criteria below.
How browsers resolve relative links
When enterprises migrate documents to a new domain, the most common symptoms are broken images and incorrect link redirects. Browsers resolve relative paths such as images/logo.png according to RFC 3986:
- Look for a
<base href="...">tag first. - If there is none, resolve the path against the current document's URL.
If an exported static HTML file has neither a correct <base> tag nor its original directory structure, every relative reference points to the root of the new domain or to the current page's path. This results in resource 404.
Why urljoin and the <base> tag behave differently
urllib.parse.urljoin(base, url) is the core tool for rewriting links, but it does not behave exactly like browser <base> handling. These differences cause many scripts to work only after someone patches them.
urljointreats the base as a file by default. Unless the base ends with/, its last segment is replaced. For example,urljoin("https://new.com/docs", "img.png")returnshttps://new.com/img.png(docsis discarded as a file name), not the expectedhttps://new.com/docs/img.png.- Browsers are more tolerant of non-standard input. HTML
<base>parsing also relies on trailing slashes to tell files from directories, but it accepts slightly more malformed values. - Absolute paths and full URLs are handled separately. Paths starting with
/and links with a full protocol are returned as-is or have only the hostname replaced. This is safer than string concatenation, but you must decide clearly which values to rewrite and which to preserve.
Preprocess the base URL
Do not assume your input is valid. Before calling urljoin:
- Check that the scheme is valid.
- Append the directory suffix
/where the base should be a directory. - Filter out null values.
Only standardised base URLs make reliable inputs.
Self-contained synthetic example
The example below lists its dependencies in the import list. It builds its inputs in memory, so it needs no files or network access.
from urllib.parse import urljoin, urlsplit
from bs4 import BeautifulSoup
def rewrite(markup, page_url):
soup = BeautifulSoup(markup, "html.parser")
for tag, attr in [("a", "href"), ("img", "src")]:
for node in soup.find_all(tag):
value = node.get(attr)
if not value or value.startswith("#"):
continue
result = urljoin(page_url, value)
if urlsplit(result).scheme not in {"https", "http"}:
del node[attr]
else:
node[attr] = result
return soup
result = rewrite('<img src="logo.png"><a href="../guide/">Guide</a><a href="#top">Top</a><a href="javascript:alert(1)">Bad</a>', "https://example.com/docs/page.html")
assert result.img["src"] == "https://example.com/docs/logo.png"
assert result.find_all("a")[0]["href"] == "https://example.com/guide/"
assert result.find_all("a")[1]["href"] == "#top"
assert "href" not in result.find_all("a")[2].attrs
Rewriting resource links safely
The example shows how to rewrite resource links in HTML safely.
Note: This example demonstrates the logic; a production version needs more checks. It uses in-memory strings instead of file system operations and network requests, so verify the logic in a controlled environment before you rely on it.
The example handles only a[href] and img[src]. It does not override CSS, srcset or <base> tags, and rewriting URLs is not part of HTML sanitisation.
Acceptance criteria and the manual review boundary
Automation can fix mechanical path-joining problems, but it cannot replace judgement about business meaning. The table below lists common exceptions and how to handle them. It helps your team separate cases the code can resolve from cases that need a person.
| Observation | What should be done | Should not be assumed |
|---|---|---|
| File-type page URL | Keep the original baseline | That a trailing slash is needed |
| Fragment anchor | Keep the reference to this page | That it must point to a remote page |
| Script protocol | Remove the attributes, as the example does | That removing them makes the HTML fully secure; complete HTML still needs security sanitisation |
Scope of non-applicability: This method applies only to rewriting attributes in static HTML. Links generated at runtime by JavaScript, references inside Shadow DOM, CSS background-image URLs, and Canvas or WebGL texture paths are not visible to BeautifulSoup. Test these in a real browser, or use Playwright or Puppeteer for end-to-end visual regression testing. Code demonstrations do not prove delivery quality. Final acceptance must rest on the rendering produced by the user agent.
Next steps
- Add assertion checks for rewritten links to your CI/CD pipeline.
- Sample historically exported documents and check that their resources are accessible.
- If dynamic content links keep causing problems, consider a headless browser for visual regression testing.
Treat any automated fix as a supporting measure, not a silver bullet.
Using this checklist for document translation
Process the body text, links, code and structure of a web document separately. Put the protected sections in your acceptance checklist, then check readability and page display after translation.
OmniDoc translates PDF, Word, PowerPoint and Excel files: scanned pages are recognized first, table cells are translated one by one so tables keep their rows and columns, and a PDF comes back as both a bilingual and a translation-only file. Paid translation adds a glossary and context translation.
References
- Beautiful Soup Official Documentation - Attributes: https://www.crummy.com/software/BeautifulSoup/bs4/doc/#attributes
- Python urllib.parse.urljoin documentation: https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urljoin
- RFC 3986 URI Generic Syntax: https://datatracker.ietf.org/doc/html/rfc3986
🎯 Which approach fits you?
If you are moving static HTML to a new domain
Start with the base URL and the urljoin rules, then verify that the rewritten links resolve.
If your pages depend on JavaScript or Shadow DOM
Script-based rewriting will miss these references, so test them in a real browser or with Playwright or Puppeteer.
If you are translating web documents
Protect the body text, links, code and structure separately, and check the layout after translation.
❓ FAQ
1. Why does urljoin drop the last part of my base URL?
Without a trailing slash, urljoin treats the last segment as a file name and replaces it. Append / to the base if it is a directory.
2. Does this method fix links generated by JavaScript?
No. It applies only to static HTML attributes. Runtime links need testing in a real browser.
3. Should I change fragment anchors when I move the page?
No. Keep them pointing to the current page.
4. Is the example ready for production use?
No. Verify the logic on copies of your own files first.
📝 Summary
Broken images after a domain move usually come from lost parsing context, not lost files. Set the correct baseline, normalise base URLs before joining, and confirm the results in a real browser.
Try it now: upload a document and get it translated with the layout intact


