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?
Hello,
You are correct, redirecting based on the User-Agent header is not possible with Page Rules. However, this is a perfect use case for Cloudflare Workers!
Cloudflare Workers is a powerful serverless platform that allows you to run JavaScript code at the edge of our network.
You can use a simple Worker script to inspect the User-Agent header of an incoming request and issue a redirect if it matches a specific pattern.
Here is an example script that would redirect any visitor with "MSIE 8.0" in their User-Agent to a different page:
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('http://example.com/unsupported-browser.html', 301)
}
return fetch(request)
}
You can add this script to a new Worker in the Cloudflare dashboard and then assign it to a route (e.g., *example.com/*) to have it run on all requests to your site.
Let me know if you have any questions about setting this up.
Best regards,
João Estêvão
Customer Support Engineer
Cloudflare