Cloudflare Pages: get rid of redundat 308 redirect

@Walshy @matteo It’s been half a year since last update on this topic. Is there at least any workaround? I’m trying to use workers to prevent the redirect, but no luck. Can we expect this redundant redirect being addressed in the next few months or we should forget about it and use other tools for deploying statically generated websites? I love Cloudflare and wouldn’t want to pull the plug.

1 Like

Ok I found a decent workaround: I created a routed proxy worker that basically adds trailing slash in the request, fetches data from * with that slash at the end to prevent extra redirects, then sets proper content type on response for assets (default header returns text/html for css and js…) and returns the updated response with all other headers passed from the original * response.

simplified wrangler.toml can look like this:

name = "example-website-proxy"
main = "worker.ts"
compatibility_date = "2022-08-23"
account_id = "xxx"
route = "*"

If anyone is interested how the worker file should look like, let me know - I’ll create a simplified version.

Deploying via wrangler publish works like a charm.

@msajnog93 I’d like to see what you have setup if you don’t mind sharing we have the same issue.

We are facing something we assume is related and was first reported here

and that is that /pages/test.html gets a 308 to /pages/test. We assume this behavior must be related to this issue since otherwise there could be conflicts. On the other hand it can also serve as a very crude workaround for OPs issue since the way it’s implemented non trailing slash URLs seem to be reserved for permalink of html files.

It’s difficult to understand how the decision was made to have a default that serves anything else then just the file as is. It’s great to give the option to configure permalinks but it should be as an option.

Due to the issues listed by OP and the work required for migration, e.g. CI & CB tools and scripts, this is preventing us from migrating to Cloudflare Pages compared to alternatives so we added our vote as well to the feature request.

Any news? This is preventing Google from indexing many of my website pages.

I just deployed a static .html site and ran into similar behavior where CF is redirecting to a non-.html link (that I never even deployed, it just magically appeared?). This is extremely unintuitive given I never configured it this way and it makes everything slower by adding additional RRTs to every page request.

Please add an option to turn off these unwanted redirects.

This blog gave the workaround I needed (I use Gatsby as a static site builder, but this should work for any statically built site):

By moving every index.html file up a directory, the Cloudflare redirect rules result in the non-trailing-slash URL I needed.

The bash script used is below:

find . -name 'index.html' -mindepth 2 -type f -exec sh -c '
    parent="$(dirname "$1")";
    mv "$1" "$parent/../$(basename "$parent").html";
' find-sh {}

The blog has a table of the redirect behavior of Cloudflare, if you mutate your files there should be a solution that gives the desired canonical URLs.

2023 is here. Any updates on this issue?

No update yet. Other work has taken priority. More configuration is on the roadmap for this year but I don’t have any other timeline than that right now.

Hello. I think, Ihave the same issue, but not sure.
I have static website with URLs like /about.html , /contact.html etc. But Cloudflare uses 301-redirect to /about and /contact. I started at Cloudflare Pages and continued when I had moved to Github+Netlify. Normalize incoming URLs is turned off. How can I use original URLs with .html ?

1 Like

I was also getting perpetual 308 redirects when setting up my new site with Cloudflare dns.
I found the fix (for me) here:

Basically just go to Cloudflare SSL/TLS section, Overview page, switch the SSL/TLS encryption mode from “Flexible” to “Full”.

1 Like

This script worked great for me! I made a gist with some instructions on how I used this to fix the issue for us.

1 Like

Is there any update on this issue at all or any indication that Cloudflare intends on solving this issue? This is a major blocker for large organizations looking to migrate to Cloudflare, and folks who want to use the Pages product for their existing projects without destroying their SEO.

It is now 2023 and this issue has been open for nearly two years now. Just flicking through this tread you can see how many folks are here looking and hoping for answers with nothing tangible amounting from this discussion.

I hope that work around this issue can finally be prioritized as I am one more user now looking for Cloudflare alternatives due to this single issue.

1 Like

Directly tagging Cloudflare folks for visibility on this issue:

@Walshy @matteo

Hi, I had/have a similar problem.

Found a temporary workaround for my Nuxt 3 project (SSR - Static Generation) by making the following configuration in nuxt.config.ts:

nitro: {
  prerender: {
    // Insert each route here with a trailing slash
    routes: ['/', '/route-1/', '/route-2/', '/and-so-on/']

Definitely not a great solution, but maybe it will help someone who has the same setup as me.

Possible to get an update/fix for that? I’m going to have to leave cloudflare pages because of this issue, it’s damaging SEO because of al the redirects

The same problem also causes trouble for me. It blocks browsers like w3m from viewing pages normally because of the redirects.