Add Cloudflare Pages middleware to handle trailing slash redirects#1631
Merged
Conversation
- Created edge function that adds trailing slashes before redirect processing - Fixes issue where URLs without slashes don't match redirect rules - Temporary solution until migration off Cloudflare Pages - Includes documentation and testing instructions - Preserve query parameter and hashes (anchors) when redirecting - Detect and handle file extensions
Deploying docs with
|
| Latest commit: |
1cef038
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://7cf51575.docodile.pages.dev |
| Branch Preview URL: | https://cloudflare-trailing-slash-ed.docodile.pages.dev |
johndmulhausen
approved these changes
Sep 11, 2025
mdlinville
added a commit
that referenced
this pull request
Sep 11, 2025
…rects" (#1633) Reverts #1631 With this change, docs are down with a Cloudflare error error: Please check back later Error 1027 This website has been temporarily rate limited You cannot access this site because the owner has reached their plan limits. Check back later once traffic has gone down. If you are owner of this website, prevent this from happening again by upgrading your plan on the [Cloudflare Workers dashboard](https://dash.cloudflare.com/?account=workers/plans). [Learn more about this issue →](https://developers.cloudflare.com/workers/about/limits/#number-of-requests-limit)
mdlinville
added a commit
that referenced
this pull request
Sep 11, 2025
Improves on the fix deployed in #1631 so that the function fires only when the URL is missing the trailing slash and would result in a 404. Should reduce the likelihood of hitting Cloudflare rate limiting, while slightly increasing the likelihood of getting an obscure 404 if it is not in one of the top level directories we are considering.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Cloudflare Pages has a limitation where:
/wandb/config/→ redirects correctly to/ref/python/sdk/functions/init/(via _redirects)/wandb/config→ returns 404 (doesn't match the redirect rule)Recently, we discovered that this results in a Google Search Console report with over 1000 404s that never hit the Cloudflare redirects at all.
Solution
We've implemented a Cloudflare Pages Function middleware that automatically adds trailing slashes to URLs before they're processed by the redirect rules.
How it works
Example flow
/wandb/config/wandb/config/(301)/ref/python/sdk/functions/init/(301)/ref/python/sdk/functions/init/Implementation
Files created/modified:
/functions/_middleware.js- The edge function that handles trailing slash redirects/public/_routes.json- Configuration to ensure the middleware runs on all routesTested Scenarios
✅ Working correctly:
Redirect rules without trailing slashes
/wandb/config→/wandb/config/→/ref/python/sdk/functions/init//wandb/config?test=param→/wandb/config/?test=paramFiles with extensions (not redirected)
/wandb/config.html→ 404 (no redirect)/style.css→ served as-is/script.js→ served as-isPaths already with trailing slashes (passed through)
/index/→ processed normally/wandb/config/→ triggers redirect rule directlySystem files (not redirected)
/robots.txt→ served as-is/favicon.ico→ served as-isDirectory paths
/guides→/guides/(when directory exists)Special paths
/→ works correctly (already has slash)/path.with.dots→ processed normally (no common file extension)Edge cases handled:
Deployment Instructions
The middleware will be automatically deployed to production when this PR is merged. These files are the key:
/functions/_middleware.js- The middleware code/public/_routes.json- Routes configuration (optional, but recommended)Testing
This has been tested locally by using Wrangler, since it's not so easy to test it in the Hugo layer.
The middleware is deployed in this PR's preview build for easy testing. The Cloudflare build logs in this PR show:
Test these URLs without the trailing slash, and they should redirect as described:
If you are observing in the browser inspector or the Cloudflare logs, each test should result in two redirects:
/wandb/config→/wandb/config/)/wandb/config/→/ref/python/sdk/functions/init/)