Worker code to manage redirects

Hi community,
I’m new with worker usage and I’m trying to migrate some Apache HTTPD reverse proxy pass directives into cloudflare and I’m using worker for the scope.
All is working as expected with exception of cases where the origin is responding with a 302/301 redirect code.
In this case the worker is transferring to the client browser the 302 code and the location communicated by the origin and in the next call the client browser is pointing directly to the external location not crossing anymore my worker.
Apache reverse proxy pass directive is instead rewriting the FQDN coming from the origin with the worker FQDN making the proxy completly trasparent.
Is there a way to get exactly the same behavior with the worker?
I have done many search on the internet and in the community but with no success, probably I’m not using the right keywords. I really appreciate everyone can indicate the right way. Thanks in advance!

Hi there,

I’m not sure I understand your requirements. Is the goal to follow the redirect with the worker instead?
If so, you can specify that redirects should be followed. Here’s an example:

addEventListener('fetch', event => {

async function handleRequest(incomingRequest) {
    const outgoingRequest = new Request(
        new Request(incomingRequest, {
            redirect: 'follow'

    return await fetch(outgoingRequest);

Related documentation:

The redirect mode to use: follow, error, or manual. The fetch method will automatically follow redirects if the redirect mode is set to follow. If set to manual, the 3xx redirect response will be returned to the caller as-is. The default for a new Request object is follow. Note, however, that the incoming Request property of a FetchEvent will have redirect mode manual.

Take care.

Hi @mcorreia and thanks for your replay. Yes this is what I did too but I have a different behaviour with respect to Apache httpd reverse prxy pass directive where the 302 code and the redirected URL is sent to the browser client after automatic rewrite of the FQDN in the URL with the worker FQDN. For example suppose the worker is answering at and for the path /test routing to an origin backend server that is returning a 302 code with location URL In this case with apache httpd reverse proxy the code 302 is sent back to the client browser with location changed in how explained here:

How to reproduce this behaviour with the worker?
Thank you again.

Hi there,

Have you considered HTML rewriter?

Take care.

Hi there, today I tried to use Rewrite links but when the response is 302 (redirect) the response is empty and there is no HTML to rewrite.
Any additional suggestion? Thank you so much for your support.