Why Relative Links in HTML Email Break

Learn why relative links in HTML email fail, how to generate absolute destinations, and how to test delivered campaign and unsubscribe links.

Published
Reading time
6 min read
Why Relative Links in HTML Email Break
On this page

Relative links in HTML email can work in a browser preview and still fail after delivery. Unlike a page hosted on your website, an email does not necessarily have your website’s URL as its base. This guide explains how resolution works, how to generate reliable destinations, and how to check the links recipients actually receive.

Key Takeaways

  • Use absolute HTTPS URLs for web destinations in email, including navigation, campaign, and unsubscribe links.
  • A relative URL needs a base URL. In a delivered email, that base may differ from the one used by your browser preview.
  • Build template links from a controlled origin, encode URL components, and then HTML-escape the finished URL for its attribute.
  • Send real test messages: preview rendering alone cannot establish how a delivered message or its rewritten links will behave.

A relative reference such as offers does not specify an origin. A browser resolves it against a base URL. For example, if a page’s base is https://example.com/newsletters/spring/, the reference offers resolves to https://example.com/newsletters/spring/offers. The leading slash in /offers instead resolves from the origin’s root: https://example.com/offers.

You can reproduce the distinction in a browser console or a JavaScript runtime that supports the URL API:

const base = "https://example.com/newsletters/spring/";

console.log(new URL("offers", base).href);
console.log(new URL("/offers", base).href);

The first expression is expected to resolve beneath /newsletters/spring/; the second is expected to resolve beneath the origin root. The trailing slash on the base matters: without it, the last path segment is treated differently during resolution.

In an email, the message may be displayed inside a webmail application or by a native client rather than at your campaign page’s URL. The client or sending platform may also transform, remove, or otherwise handle links differently. Do not assume a relative reference will resolve against your website—or even that every client will handle it the same way.

This article’s examples were not executed, and no email clients or platform documentation were available for verification in this run. Test the actual sending platform and clients used by your audience.

Advertisement

Replace relative destinations with absolute URLs

Give each website link a complete HTTPS destination:

<!-- Fragile: depends on an unspecified base URL -->
<a href="/offers">View offers</a>

<!-- Explicit destination -->
<a href="https://example.com/offers">View offers</a>

Here example.com is an illustrative domain; replace it with the domain you control. An absolute URL specifies the scheme and host as well as the path. It does not guarantee that the destination is available or that a client will not rewrite the link, so a delivered-message test is still necessary.

Avoid protocol-relative URLs such as //example.com/offers: they still depend on the surrounding context to supply a scheme. Also avoid relying on a <base> element to repair relative email links; client handling and message transformations can differ.

Reusable templates often combine a base URL, a campaign identifier, and tracking parameters. Construct the URL before inserting it into HTML. Encode individual path or query values as appropriate, then escape the completed URL for a quoted HTML attribute. These are separate operations: URL encoding preserves data within a URL; HTML escaping preserves the attribute’s markup.

This illustrative JavaScript snippet builds a link from a fixed origin. It uses an example campaign value rather than recipient data:

function escapeHtmlAttribute(value) {
  return value
    .replace(/&/g, "&amp;")
    .replace(/"/g, "&quot;")
    .replace(/</g, "&lt;")
    .replace(/>/g, "&gt;");
}

function campaignLink(campaign) {
  const url = new URL("https://example.com/offers");
  url.searchParams.set("campaign", campaign);
  return escapeHtmlAttribute(url.href);
}

const href = campaignLink("spring sale");
const html = `<a href="${href}">View offers</a>`;

Use the equivalent URL-building and HTML-escaping facilities in your template system. Check whether the renderer already escapes attribute values so you do not double-escape them. If a template variable can provide an entire destination, validate its scheme and destination against your application’s policy before rendering; a fixed origin with separately encoded parameters is easier to control.

Do not put sensitive personal information in URL parameters. Links can appear in logs, browser history, forwarding, analytics systems, and other places outside the message.

Make unsubscribe destinations explicit

Use the unsubscribe URL provided or generated by your sending system as a complete destination, rather than appending a relative path in the template. The link must identify the appropriate subscription and lead to the intended unsubscribe experience. Treat any recipient-specific token in that URL as sensitive: avoid publishing test messages with real tokens or copying them into public bug reports.

Check your email platform’s current official documentation for its template variables, link rewriting, and unsubscribe configuration. Also check the requirements applicable to your messages and recipients; an unsubscribe link in the body and any automated unsubscribe mechanism are separate implementation concerns. Link scanners may visit links, so design and test the unsubscribe flow with automated visits in mind rather than assuming every visit represents a deliberate human action.

Advertisement

Test the delivered message, not just the preview

A browser preview is useful for spotting markup problems, but it may use your site or preview application as the base URL. It also may not exercise the sending platform’s variable substitution, tracking redirects, or delivery-time transformations.

No webmail or mobile client was tested for this article. To verify your own campaign, send a sample through the same platform and configuration used for production, using test accounts and non-sensitive data:

  1. Include a normal campaign link, a link built from a template variable, and the actual test unsubscribe link. Use explicit HTTPS destinations in the rendered HTML.
  2. Open the delivered message in at least one webmail client, such as Gmail in a browser, and one mobile email client, such as Gmail on Android. Record the client, device or browser, and test date.
  3. Inspect the rendered destination where the client permits it, then follow each link. Confirm the final page, query parameters, and unsubscribe experience are correct. If tracking is enabled, check the final destination after redirects as well as the link in the message.
  4. Inspect the delivered message source or the platform’s rendered output when available. Look for unresolved template tokens, relative href values, malformed escaping, and unexpected destinations.
  5. Repeat with empty, special-character, and missing template values. Confirm that a missing destination fails your campaign checks instead of producing an empty or broken link.

A redirect or rewritten tracking domain can be legitimate, but it should be expected and controlled by your sending setup. Test with tracking both as configured for the campaign and, when useful for diagnosis, disabled in a separate test.

Common Mistakes

  • Using /path and assuming it means your website. It means the root of whichever base origin is used.
  • Trusting a preview alone. The delivered message may have different link transformations and client behavior.
  • Concatenating unescaped variables into href. Special characters can alter the URL or break the HTML attribute.
  • Encoding the entire URL as one component. This can encode structural characters that should remain part of the URL. Build the URL first, then HTML-escape it.
  • Testing only the visible link text. Check the actual destination and the final page after redirects.
  • Publishing a real unsubscribe token in a test fixture. Use dedicated test recipients and keep their links out of public reports.

Best Practices

Maintain a controlled base origin for campaign URLs, generate complete destinations before rendering, and validate them as part of your campaign review. Keep a small test matrix covering your major client environments, and repeat it after changes to templates, sending configuration, or tracking. Consult your provider’s current documentation for platform-specific behavior rather than assuming all email systems render and rewrite links alike.

Conclusion

Relative links depend on a base that a delivered email cannot safely assume. Generate absolute HTTPS destinations, handle template variables in the right order, and verify the received message in both webmail and mobile clients before sending a campaign.

What would your domain sell for?

A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.