I’m not a developer (designer really) but I’m needing to setup a worker that will redirect to a specific location if 2 items are true.
- I need a specific UTM parameter in the URL to be found.
- I need the page in question to return a status of 404.
If both of those exists then redirect to a new pre-determined URL.
Currently it is redirecting on any page not found and I only need it to do that if the page is not found and the UTM is in the URL.
Here’s the code:
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const response = await fetch(request);
const params = new URLSearchParams(request);
if ( params.getAll('geo-targetly') && response.status === 404) {
const base = "https://www.newsite.com/";
const statusCode = 301;
const destinationURL = `${base}`;
console.log(destinationURL);
return Response.redirect(destinationURL, statusCode);
}
return response;
}
Any help is appreciated.