Help Needed: Blogger URL Rewriting Issue with Cloudflare Proxy

Hello Cloudflare Community!

I’m facing an issue with URL rewriting on my Blogger site while using Cloudflare’s proxy service, and I’m seeking some guidance and advice to resolve this problem. Your expertise and assistance would be greatly appreciated.

Background:
I have a Blogger site, and I wanted to create clean and pretty URLs similar to WordPress, where I can remove the year, month, and .html extension from my post URLs. To achieve this, I’ve set up a Cloudflare Worker to rewrite the URLs as recommended in this previous thread.

The Issue:
After implementing the Cloudflare Worker, my Blogger site began to experience a “The page isn’t redirecting properly” error. This error suggests that there might be a redirection loop or some other problem with the way Cloudflare is handling the URL rewriting.

What I’ve Tried:
I’ve done extensive troubleshooting on my end, including:

  1. Reviewing the Cloudflare Worker code to ensure it’s correctly handling the URL rewriting logic.
  2. Checking for potential redirect loops within the script.
  3. Disabling caching for testing purposes.
  4. Clearing browser caches and testing in incognito mode.
  5. Verifying that my Blogger settings are not causing conflicts.
  6. Examining Blogger templates for any unexpected JavaScript or meta-refresh tags.

Additional Info:
When I disabled the Cloudflare proxy and accessed my Blogger site directly, the issue disappeared, which leads me to believe that there might be an issue on the Cloudflare side of the configuration.

Worker.js

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const url = new URL(request.url)
  const path = url.pathname

  // Remove the year and month and .html extension from the path
  const newPath = path.replace(/\/\d{4}\/\d{2}\//, '/').replace(/\.html$/, '')

  // Construct the new URL
  const newURL = `https://www.example.com${newPath}`

  // Create a response that redirects to the new URL
  return Response.redirect(newURL, 301)
}

I would appreciate any guidance, suggestions, or possible solutions you can offer to help me achieve the desired URL rewriting without encountering the redirection error.