Resolving to staging website based on the visitor ip

I cloned my website to another host for staging purposes and tried to use the Cloudflare worker to resolve the website from the staging host for my static ip, my home ip, but was unsuccessful. Here is the script. Any idea what could be the problem?
And if the script is fine, how should I do the dns part? Because I cant give the domain DNS A record the worker url!

const stagingIP = ‘StagingHostIP’;
const productionIP = ‘ProductionHostIP’;

const allowedIPs = [‘MyHomeIP’];

addEventListener(‘fetch’, event => {



async function handleRequest(request) {

const clientIP = request.headers.get(‘CF-Connecting-IP’);

let url = request.url;

if (allowedIPs.includes(clientIP)) {
url = url.replace(productionIP, stagingIP);
} else {
url = url.replace(stagingIP, productionIP);

const init = {
headers: request.headers,
cf: {
resolveOverride: url,

return fetch(request, init);


Instead of using a Worker script you can try the new Origin Rules under the Rules menu
Origin Rules (beta) · Cloudflare Rules docs

It is not available in “pro” plans.

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