Technical Context:
I'm building a website using Astro v5 deployed to Cloudflare Workers (NOT Cloudflare Pages). My domain is registered through Cloudflare and fully proxied. I need to implement 301 redirects from non-trailing-slash URLs to trailing-slash URLs.
Current Situation:
- Cloudflare's default behavior produces 307 (Temporary) redirects, but I need 301 (Permanent) for SEO
- I've tested
"html_handling": "force-trailing-slash"in wrangler.json - it still produces 307 redirects - The 307 redirects only work for browsers, not programmatic access (curl, APIs, bots)
- I already have Page Rules for www→non-www and http→https redirects using 301s
Requirements:
- Implement 301 redirects from
/pageto/page/ - Must work for both browser and programmatic access
- Must NOT redirect files like:
/robots.txt/sitemap.xml- Any files with extensions (
.js,.css,.jpg, etc.) - API endpoints
- Cloudflare system paths (
/cdn-cgi/*) - Astro system paths (
/_actions/*,/_server-islands/*)
Specific Questions:
- What's the most reliable way to implement this - Cloudflare Redirect Rules, Worker code, or another method?
- What are ALL the edge cases and files I need to exclude from trailing slash redirects?
- Will this create conflicts with my existing www→non-www Page Rules?
- How can I avoid redirect loops?
- What specific patterns/code would you recommend for maximum reliability?
Approach I'm considering:
Expression: not ends_with(http.request.uri.path, "/") and http.request.uri.path != "/"
Target URL: concat(http.request.uri.path, "/")
Status: 301 Permanent Redirect
Please provide:
- The most reliable implementation method
- A comprehensive list of exclusions
- How to handle conflicts with other redirects
- Any gotchas specific to Astro v5 + Cloudflare Workers
- Testing strategies to ensure nothing breaks