How to Implement 301 Redirects for Your Website Without a Server (CFCDN Edition)

Sometimes it's really inconvenient to not have a server for redirects, so I found a server-free 301 redirect method using Cloudflare's Workers feature.

Gugu Gaga

As long as you put your website on Cloudflare’s CDN, you can achieve server-free 301 redirects.

Step 1

First, find the Worker or Pages section. Of course, we only need Workers.

Click Create Application

Here you can click Create Application, then copy and paste the code below into it.

1
2
3
4
5
6
7
export default {
    async fetch(request) {
        const url = new URL(request.url);
            const newUrl = "https://your-website-here.com" + url.pathname + url.search;
                return Response.redirect(newUrl, 301);
                  }
                  } 

Since my coding skills are somewhat lacking, just bear with the indentation. (Cloudflare can still run it.)

Questionable indentation

Step 2

Here you can add the domain that needs to be redirected.

Add the domain you want to redirect

After adding it, go to the Cloudflare DNS panel and make sure the orange cloud (proxy) is enabled and the corresponding records exist.

Orange cloud enabled with corresponding records

And that’s it — the 301 redirect is now complete.

The code means that it redirects from the old address to the new one while preserving the path and query string (whatever that is). The reason I wrote this article is that my GSC and Yandex site migrations simply would not work. After tinkering for a while, I realized both required NGINX, and then I thought — wait, isn’t Workers also a kind of server? I gave it a try and it actually worked. ()

Yandex Google?

Why, bro? Doesn’t this method count as a 301?

But other website validators do confirm it’s a 301 redirect — I have no idea what Google’s problem is.

1 2 3