Header Image

My Website Header

Subject: 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?

ANSWER

You're absolutely right — Cloudflare Page Rules do not support redirection based on the User-Agent header. However, there are a few alternative approaches you can consider using Cloudflare Workers, which provide much more flexibility and allow you to inspect and act on request headers, including the User-Agent.

Solution: Use Cloudflare Workers Cloudflare Workers allow you to write JavaScript that runs at the edge, giving you full control over HTTP requests and responses. The following Reddit post talks about this matter> Redirection based on user agent? how to ? Here’s a basic example of how you could redirect users with a specific User-Agent (e.g., MSIE 8.0):

addEventListener('fetch', event => {

event.respondWith(handleRequest(event.request))

})

async function handleRequest(request) {

const userAgent = request.headers.get('User-Agent') || ''

if (userAgent.includes('MSIE 8.0')) {

return Response.redirect('https://example.com/ie8-unsupported', 302)

}

// Proceed with the original request

return fetch(request)

}

Steps to Deploy This:

Log in to Cloudflare Dashboard

Go to Workers & Pages > Create a Worker

Paste the code above into the editor

Save and deploy the Worker

Set up a route (e.g., example.com/*) to apply the Worker to your site

Notes:

You can customize the User-Agent check to match other browsers or versions.

You can also use regex for more complex matching.

Make sure to test thoroughly, as redirecting based on User-Agent can sometimes lead to unexpected behavior.