Cloudflare

6. Redirection based on user agent

I want to redirect visitor with specific user agent (*MSIE 8.0* for example) to a different page. It seems this is not possible with Cloudflare Page Rules. Is there any other way to achieve that using Cloudflare?


Dear User,

Thank you for reaching out about your questions related to redirection based on User-Agent. I'd like to help you with.

To redirect visitors with a specific user agent using Cloudflare, you're correct that Page Rules alone won't be sufficient. However, you can achieve this using Cloudflare Workers. Here's how you can implement this:

1. Create a Cloudflare Worker: Log into your Cloudflare dashboard, go to the "Workers" section, and create a new Worker.

2. Use the following script as a starting point:

addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
const userAgent = request.headers.get('User-Agent') || '';

// Check if the User-Agent contains "*MSIE 8.0*"
if (userAgent.includes('*MSIE 8.0*')) {
return Response.redirect('https://example.com/legacy-browser', 302);
}

// Continue to the original request if the User-Agent does not match
return fetch(request);
}

3. Deploy the Worker: Save and deploy the Worker to your website

4. Set up a route: In the Cloudflare dashboard, go to the "Workers" section and add a route for your website (e.g., *.example.com/*) and assign it to the Worker you just created.

https://developers.cloudflare.com/workers/examples/conditional-response/

Please feel free to contact me if you need any further information.

Best regards,
Szymon KamiƄski
Cloudflare Support