Cloudflare Worker 301 Redirect Issue - Need Assistance


Currently, I have a blog hosted on the Blogspot platform, and I have many 404 URLs that I want to redirect to new URLs.

I am using Cloudflare Worker to do this, and I have already tested it via the test menu on the Cloudflare Worker, and the previously 404 URLs have been successfully redirected to the new URLs with a 301 status.

However, when I access it via a web browser, the page still shows a 404 status, while using curl, the URL returns a status of 200 (blank).

curl -I
HTTP/1.1 200 OK
Date: Sun, 22 Oct 2023 08:42:57 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
CF-Ray: 81a0732d59063e3d-SIN
CF-Cache-Status: DYNAMIC
Cache-Control: private, max-age=0
Expires: Sun, 22 Oct 2023 08:42:57 GMT
Last-Modified: Wed, 18 Oct 2023 06:07:49 GMT
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
Report-To: {"endpoints":[{"url":"https:\/\/\/report\/v3?s=1FtyByJ41iyyC6xPx99SjXwqBoj3GY1oubfGGOGZhVfLeXnEAhtlVOBii%2FVT7RojglZ%2BZ4zVGRwXMXiZ%2BeSjBe0n%2BcuCuDbvzTs2UY3UHs2jDAa6q449wBO8wOBmhgluV%2FdcyaGVlw%3D%3D"}],"group":"cf-nel","max_age":604800}
NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
Server: cloudflare
alt-svc: h3=":443"; ma=86400

The Cloudflare Worker code I am using is as follows:

addEventListener('fetch', event => {

async function handleRequest(request) {
  const redirectMap = new Map([
    ['/manfaat-burung-merpati-sebagai-alat.html', '/2019/06/manfaat-burung-merpati-sebagai-alat.html'],

  const requestURL = new URL(request.url)
  const requestPath = requestURL.pathname

  if (redirectMap.has(requestPath)) {
    // If there's a mapping, construct the new URL
    const newURL = `${redirectMap.get(requestPath)}`

    return Response.redirect(newURL, 301)

  return new Response('404 Not Found', { status: 404, statusText: 'Not Found' })

It seems there might be something wrong with the steps I have taken. Can anyone help me identify my mistake?

Thank you.

What’s the route you setup for the Worker?

I’m not sure I understand, but I believe you want to map a certain key path

I Just Run this code in a Cloudflare Playground

And if you pass the path you want, I believe it will be redirected to the value of the key path in question.

 * @typedef {Object} Env

export default {
	 * @param {Request} request
	 * @param {Env} env
	 * @param {ExecutionContext} ctx
	 * @returns {Promise<Response>}

	async fetch(request, env, ctx) {

		const redirectMap = new Map([['/manfaat-burung-merpati-sebagai-alat.html', '/2019/06/manfaat-burung-merpati-sebagai-alat.html']])
		const url = new URL(request.url);

		const requestPath = url.pathname

		if (redirectMap.has(requestPath)) {
			// If there's a mapping, construct the new URL
			const newURL = `${redirectMap.get(requestPath)}`

			return Response.redirect(newURL, 301)

		return new Response('404 Not Found', { status: 404, statusText: 'Not Found' })




my route is set to **

Yes, the code in the playground works as expected. However, on the actual page with errors, the one I tested in the playground didn’t produce an HTTP status header of 301; the page still returned a 404 error. When I tested it using curl, the result was 200 OK, just as I mentioned at the beginning of the thread.

In the worker summary, I noticed that there were incoming requests registered by the Worker script.


is it the same code as above? Can you send the url assigned to your worker (* here?

no ,its not the same

here you go

It looks like it’s working as it should, right?

The request pathname to be passed in the worker url is redirecting to the value :


worker url:

worker url with pathname (url + pathname) :


Is this not what is intended?

Yup … as simple as I mentioned earlier, the goal is to redirect 404 URLs to a new page.

The steps I took are as follows…

I created a Cloudflare worker with the above code, and my route is set to **

It seems like the code in the Cloudflare worker is running as it should (both the code I initially pasted and the code you provided) when we test it in the worker playground.

The issue arises in the real-world implementation… the URL that was previously a 404 should now have a 301 status, but as I mentioned above, the URL is still a 404 when accessed through a web browser and returns a 200 OK when using curl.

using online url checker :

its status also returning 200 OK , im expecting to have status 301 and redirected to new url

Have you tried the blogger’s own 404 handler, doing this on origin will be more effective. write a Javascript logic within the 404 error template.

Yes, bloggers do have a redirect feature, but that’s the problem. This feature doesn’t work as I expected, especially with parameters. It generates multiple unnecessary redirects or redirects to URLs I didn’t expect. That’s why I have the idea to use Cloudflare workers.

What confuses me is why Cloudflare workers produce different status headers, as I mentioned earlier.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.