Cloudflare workers work for the workers.dev domain but not for my actual domain

I have a Cloudflare worker that’s supposed to redirect my domain based on the configuration I have in my Cloudflare worker. It works successfully for the Cloudflare URL (the worker .dev url) but is not working for my domain mycloudsafari .com/blog

I just switched the nameservers over from AWS to Cloudflare and it seems to have propagated. I also have a proxy infront of my domains so it should be working.

Don’t know if this is the right place to post this, but was redirected here after trying to fill our a support question. Any help would be greatly appreciated cheers!

May I ask if you’ve correctly set the route path for the Worker? :thinking:

Is there a way to share at least a part of your Worker code which does the redirection thing?

Furthermore, I can see the IP is with 188.xxx and some users are reporting some issues with the access/connection.

Nevertheless, what I see mycloudsafari.com/blog is different than mycloudsafari.com.com/blog/ → the trailing slash led me to some “redirection loop” and kind of a strange URL in my Web browser.

https://mycloudsafari.com/blog/143.110.220.106/143.110.220.106/143.110.220.106/143.110.220.106/143.110.220.106/143.110.220.106/143.110.220.106/143.110.220.106/143.110.220.106/143.110.220.106/143.110.220.106/143.110.220.106/143.110.220.106/143.110.220.106/143.110.220.106/143.110.220.106/143.110.220.106/143.110.220.106/143.110.220.106/143.110.220.106/blog

Thanks for getting back!

These are the route paths I have setup:

This is the worker code I had set up:

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

/**
 * Respond to the request
 * @param {Request} request
 */
async function handleRequest(request) {
     try {
      
        const urlObject = new URL(request.url);


        // If the request is to the Ghost subdirectory
        if (/^\/blog/.test(urlObject.pathname)) {
          // Then Proxy to Ghost
          const GHOST_URL = "blog.mycloudsafari.com";
          const CUSTOM_URL = "mycloudsafari.com/blog"; 

          let url = new URL(request.url);

          url.hostname = GHOST_URL;

          let proxyRequest = new Request(url, request);

          proxyRequest.headers.set('Host', GHOST_URL);

          //Have an X-Forwarded-Host header that matches the custom domain in my.ghost.org.

          proxyRequest.headers.set("X-Forwarded-Host", CUSTOM_URL);

          //Include the X-Forwarded-Proto header set to https not http.

          proxyRequest.headers.set("X-Forwarded-Proto", "https");

          //Include the X-Forwarded-For header, populated with the remote IP of the original request.

          let ip = proxyRequest.headers.get("CF-Connecting-IP");

          proxyRequest.headers.set("X-Forwarded-For", ip);

          return await fetch(proxyRequest);

        }

     } catch (error) {

        // if no action found, play the regular request
        let response = await fetch(`https://mycloudsafari.com${urlObject.pathname}`);

        return await fetch(request);
    }

    return await fetch(request);
}

I spent a good three days on this, needed to move quick so for now ended up just setting the blog at blog.mycloudsafari.com via Ghost (blog). Deleted the digital ocean droplet I had initially set up with the server and nginx serving the site.