Skip to content

Trailing-slash cleanup on Cloudflare: one URL form, no duplicate content

When /page and /page/ both return 200, search engines index duplicates and the edge caches everything twice. Here is how to pick one form and enforce it with a single Redirect Rule — without looping against the CMS.

If example.com/pricing and example.com/pricing/ both answer 200, you are serving every page under two URLs: search engines split ranking signals between them, analytics splits pageviews, and Cloudflare caches each page twice because the two paths are different cache keys. The cleanup is to pick one form — usually whatever your CMS already prefers — and enforce it with a single 301 at exactly one layer. On Cloudflare that is one Redirect Rule, and the Free-plan version needs no regex.

What’s happening

Several layers have opinions about the slash, and they answer in order: the browser cache replays any 301 it stored; the Cloudflare edge applies Redirect Rules (only on proxied hostnames, before cache and origin); the origin server may add slashes to real directories (Apache’s DirectorySlash); and the CMS often normalizes last — WordPress’s redirect_canonical() adds a trailing slash to match its permalink setting, while many static-site hosts strip it. Duplicate content appears when no layer enforces a form, and loops appear when two layers enforce opposite forms: an edge rule stripping the slash while WordPress adds it back produces a perfect ping-pong.

The edge angle matters even if you only care about SEO: /pricing and /pricing/ are distinct cache keys, so each gets its own cached copy, halving your hit ratio on affected pages and doubling purge work.

What to check first

  • What does the CMS already do? Request both forms of a known page and see whether one 301s. WordPress normalizes by itself; if so, the edge may need no rule at all.
  • Which form dominates the index and the sitemap? Keep the form your sitemap, internal links, and indexed pages already use — moving the majority is self-inflicted churn.
  • Are there paths where the slash is meaningful — API routes where /v1/thing and /v1/thing/ differ, or files that must never be rewritten?

How to verify

Test both forms of a few representative URLs and see who answers:

curl -sI https://example.com/pricing  | grep -iE 'HTTP/|location|x-redirect-by|cf-cache-status'
curl -sI https://example.com/pricing/ | grep -iE 'HTTP/|location|x-redirect-by|cf-cache-status'

One of the two should 301 to the other; x-redirect-by: WordPress means the CMS did it, server: cloudflare with a cf-ray and no cf-cache-status means your edge rule did. Walk the chain to make sure normalization costs at most one hop:

curl -sIL 'https://example.com/pricing/?ref=nav' | grep -iE 'HTTP/|location'

And check the origin’s own behaviour without the edge in the way (-k for an Origin CA certificate):

curl -skI --resolve example.com:443:203.0.113.10 https://example.com/pricing/ | grep -iE 'HTTP/|location'

The fix

  1. Pick the canonical form and write it down. Match the CMS default: trailing slash for a standard WordPress permalink setup, no slash for most static and headless stacks. Consistency matters more than the choice.

  2. Enforce at one layer only. If the CMS already 301s reliably, stop here — do not add an edge rule that duplicates (or worse, reverses) it. Add the edge rule when the origin does nothing, when multiple origins disagree, or when you want the hop handled before origin load.

  3. Strip-slash rule (Free plan, no regex). Rules, Redirect Rules, custom filter expression:

    When: ends_with(http.request.uri.path, "/") and http.request.uri.path ne "/"
    Then: Dynamic redirect
      Expression: concat("https://example.com", substring(http.request.uri.path, 0, -1))
      Status: 301, preserve query string: on

    The ne "/" guard keeps the homepage out of the rule.

  4. Add-slash rule (the mirror case). Filter not ends_with(http.request.uri.path, "/") and not http.request.uri.path contains ".", dynamic target concat("https://example.com", http.request.uri.path, "/"). The dot guard excludes files like /logo.png. If you need finer matching, regex_replace exists but requires Business or Enterprise; the Rules documentation lists operator availability per plan.

  5. Clean up the artifacts. Update the sitemap and internal links to the canonical form, purge the zone cache so the duplicate cache entries die, and spot-check rel="canonical" tags now agree with the redirect.

When this fix is wrong

  • The CMS already normalizes correctly. An edge rule adds a second owner for the same decision; the failure mode is a loop, not a cleanup.
  • Slash-sensitive APIs live on the same zone. Scope the rule with a path prefix (starts_with(http.request.uri.path, "/blog")) or exclude /api/ explicitly rather than normalizing everything.
  • The duplication is scheme or host, not slash. If the variants are www/apex or http/https, fix host and scheme canonicalization first — slash cleanup on top of a broken host chain just adds hops.

Common mistakes

  • Opposite directions at two layers. Edge strips, WordPress adds — instant redirect loop. Always test one URL end to end after enabling the rule.
  • Forgetting the root exclusion, sending / into a malformed redirect.
  • No dot guard on an add-slash rule, so assets get redirected to /app.css/ and break.
  • Using a Transform Rule instead of a Redirect Rule. A rewrite changes the URL sent to the origin but not the one in the address bar, so both public forms keep existing — the duplicate-content problem survives. The difference is laid out in Transform Rules: URL rewriting.
  • Using 302. Search engines keep both URLs alive indefinitely; canonicalization needs 301.

Prevention

Decide the slash policy on day one of a project and encode it once — in the CMS settings or in one named edge rule, never both. Generate sitemaps and internal links from the same setting so they cannot drift, and keep a two-line curl check (both forms of one page) in the deploy checklist. When redirect chains do creep in over time, hunt them with the method in finding and fixing redirect chains.

If redirects keep fighting each other across Cloudflare, the origin, and the CMS, Stack Harbor untangles that chain as part of Managed Cloud Ops — book an infrastructure review.