Focux
Focux
CDCloudflare Developers
Created by Focux on 9/17/2024 in #general-help
URL origin rewrite using rule snippets
I'm trying to do an URL rewrite based on the HTTP status code, so basically: if example.com/something status equals 404, then keep showing the same example.com/something URL but show the content of example.ORG/something. This is the code I currently have and it's not working, it's instead doing a redirect to example.org/something and I don't want that. I want to mask that content under the first domain.
export default {
async fetch(request) {
const response = await fetch(request);

if (response.status === 404) {
const newRequest = new Request(request);

const url = new URL(request.url);
url.hostname = "example.ORG";

const response = await fetch(url, newRequest);
const newResponse = new Response(response.body, response);
return newResponse;
}

return response;
},
};
export default {
async fetch(request) {
const response = await fetch(request);

if (response.status === 404) {
const newRequest = new Request(request);

const url = new URL(request.url);
url.hostname = "example.ORG";

const response = await fetch(url, newRequest);
const newResponse = new Response(response.body, response);
return newResponse;
}

return response;
},
};
2 replies