#t

6 messages · Page 1 of 1 (latest)

cobalt kraken
#

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:

  1. Implement 301 redirects from /page to /page/
  2. Must work for both browser and programmatic access
  3. 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:

  1. What's the most reliable way to implement this - Cloudflare Redirect Rules, Worker code, or another method?
  2. What are ALL the edge cases and files I need to exclude from trailing slash redirects?
  3. Will this create conflicts with my existing www→non-www Page Rules?
  4. How can I avoid redirect loops?
  5. 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:

  1. The most reliable implementation method
  2. A comprehensive list of exclusions
  3. How to handle conflicts with other redirects
  4. Any gotchas specific to Astro v5 + Cloudflare Workers
  5. Testing strategies to ensure nothing breaks
tropic lintelBOT
#

Hi @cobalt kraken. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the provided knowledge sources, here’s what can be determined about implementing 301 redirects from non-trailing-slash URLs to trailing-slash URLs in an Astro v5 site deployed to Cloudflare Workers:


1. Most Reliable Implementation Method

Astro’s built-in trailingSlash config
Astro provides a trailingSlash configuration option in astro.config.mjs:

export default defineConfig({
  trailingSlash: 'always'
});

When set to 'always', Astro will, in production, redirect requests for on-demand rendered URLs without a trailing slash to the correct URL with a trailing slash using a 301 (permanent) redirect for GET requests. For all other request methods, it will use a 308 redirect. This is the intended, built-in way to handle trailing slash redirects in Astro, and it is designed to work with SSR and adapters, including Cloudflare Workers. However, prerendered pages’ trailing slash handling is left to the hosting platform and may not always respect this config. You cannot use Astro’s redirects config for this use case; it is handled internally by Astro and the platformConfiguration Reference – trailingSlash.

Cloudflare Workers custom logic
If you need more granular control (e.g., to exclude certain paths or file types), you may need to implement this logic in your Worker code. The knowledge sources do not provide a ready-made Worker snippet for this, but they do confirm that routing for static assets is based on the file structure, and unmatched requests fall back to the Worker for on-demand renderingCloudflare Platform – Routing.

#

Cloudflare Redirect Rules
The documentation mentions that you can declare custom redirects by adding a _redirects file in your public/ folder, but this is more relevant for Cloudflare Pages and not for Workers. For Workers, custom logic in the Worker script is the most flexible approachCloudflare Platform – Redirects.


2. Comprehensive List of Exclusions

The knowledge sources do not provide an exhaustive list, but based on the context and standard Astro/Cloudflare practices, you should exclude:

  • Any path ending with a file extension (e.g., .js, .css, .jpg, .png, .svg, .json, .xml, .txt, etc.)
  • /robots.txt
  • /sitemap.xml
  • API endpoints (e.g., /api/*)
  • Cloudflare system paths: /cdn-cgi/*
  • Astro system paths: /_actions/*, /_server-islands/*
  • The root path /

This matches your requirements and is consistent with the exclusions mentioned in the documentation for static asset routing and system pathsCloudflare Platform – Routing.


3. Handling Conflicts with Other Redirects

Astro’s route priority rules state that redirects will always take lower precedence than an existing page file of the same name. If you have Page Rules for www→non-www and http→https, these should be processed before your trailing slash logic, as long as the order of rules in Cloudflare is correct. The documentation does not mention any specific conflicts, but you should ensure that your trailing slash redirect is not applied to URLs that have already been redirected by other rulesRouting – Redirects.


4. Avoiding Redirect Loops

#
  • Only redirect if the path does not already end with a slash and is not /.
  • Exclude all file paths and system paths as above.
  • Ensure that your redirect does not apply to URLs that already have a trailing slash or are excluded.

Astro’s built-in handling for trailingSlash: 'always' is designed to avoid loops, but if you implement custom logic in a Worker, you must be careful to check these conditionsConfiguration Reference – trailingSlash.


5. Recommended Patterns/Code

Astro config (recommended if possible):

export default defineConfig({
  trailingSlash: 'always'
});

This will handle 301 redirects for on-demand rendered pages, but not for prerendered/static filesConfiguration Reference – trailingSlash.

Custom Worker logic (if you need more control):

The knowledge sources do not provide a full Worker code example, but your approach is on the right track. You must ensure you exclude all the necessary paths and file types.


6. Gotchas Specific to Astro v5 + Cloudflare Workers

  • Prerendered pages: Trailing slash handling for prerendered pages is left to the hosting platform and may not respect your Astro config. You may need to handle these with Worker logic or Cloudflare RulesConfiguration Reference – trailingSlash.
  • Cloudflare Workers routing: Static assets are served based on the file structure, and unmatched requests fall back to the Worker. Be careful not to interfere with static asset routingCloudflare Platform – Routing.

7. Testing Strategies

#
  • Use curl -I and similar tools to test both browser and programmatic access.
  • Test all exclusions: static files, system paths, API endpoints, etc.
  • Test with and without trailing slashes, and with query strings.
  • Test interaction with your www→non-www and http→https rules.
  • Check for redirect loops by following redirects with curl -L.

Summary:

  • Use Astro’s trailingSlash: 'always' config for on-demand rendered pages.
  • For full control (including static files), implement custom logic in your Worker, carefully excluding all file types and system paths.
  • Ensure your Cloudflare Page Rules are ordered to avoid conflicts.
  • Test thoroughly for all edge cases.

If you need a Worker code example, the knowledge sources do not provide one, so you may need to consult Cloudflare’s Worker documentation for implementation details.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: