Skip to content

Instantly share code, notes, and snippets.

@srhyne
Created March 30, 2023 19:25
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srhyne/855a4dea1394ebac03def06c8a1ba3bb to your computer and use it in GitHub Desktop.
Save srhyne/855a4dea1394ebac03def06c8a1ba3bb to your computer and use it in GitHub Desktop.
Fix Cloudflare 308 trailing comma redrect issue
# Fix Cloudflare 308 trailing comma redrect issue
# https://community.cloudflare.com/t/cloudflare-pages-get-rid-of-redundat-308-redirect/324582/29
# https://electricui.com/blog/switching-to-cloudflare-pages#how-to-disable-the-trailing-slash-on-cloudflare-pages
find ./dist -name 'index.html' -mindepth 2 -type f \
-exec sh \
-c 'parent="$(dirname "$1")"; mv "$1" "$parent/../$(basename "$parent").html";' \
find-sh {} \;
find ./dist -empty -type d -delete
@srhyne
Copy link
Author

srhyne commented Mar 30, 2023

Fix Cloudflare 308 trailing comma redrect issue

This script works by

converting => /blog/post-slug-name/index.html
to => /blog/post-slug-name.html

Logically, Cloudflare things that /blog/post-slug-name/index.html is going to be a folder including more content. So they add the trailing / comma to the end of the request and use a 308 redirect in the process! 😮‍💨

How I used

  • Put migrate-index.sh in ./src/scripts (of my CF pages project)
  • npm run build && bash ./src/scripts/migrate-index.sh

Of course you could add the migration into your npm run build directly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment