Workers - redirect PL users to /pl, all other countier/lang. to /

For Workes & Pages, what is the name of the domain?

verk.design

What is the error number?

There is no error yet :slight_smile:

What is the error message?

There is no error message yet :slight_smile:

What is the issue or error you’re encountering

I want only to get some help about configuration…

What steps have you taken to resolve the issue?

Hi!

There is no issue, error orother problem yet… I would like help configuring the service so that I can properly redirect my users.

On my page verk.design I want to configure the following:

Client from Poland - redirect to verk.design/pl

Client from all other countries/languages redirect to verk.design

–

I have read about workers and created something like this:

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

async function handleRequest(request) {
const country = request.headers.get(‘cf-ipcountry’)

if (country === ‘PL’) {
return Response.redirect(‘verk.design/pl’, 301)
} else {
return Response.redirect(‘verk.design’, 301)
}
}

–

My site is working on WP + WC with WMPL plugin. WPML config attached.
Pls. help me, if I am right with this code and how to add it propelry to Cloudf. Or maybe there is some other solution to get result as expected…

Thanks…

Screenshot of the error

Hey there,

If you want to use a worker for the redirect, then I would recommend checking out this example redirect we have in our developer documentation. You would also have to make sure that you have a worker route configured so that the worker can intercept the request.

Alternatively, you could use a redirect rule within the Cloudflare dashboard to do this redirect instead, which should be easier than configuring a worker for this. Here is an example redirect rule that is configured to redirect based on country.

Thank You, I will test the code…