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?
Response
Hi
Thanks for reaching out! There's a few ways you can achieve this:
- using our WAF User Agent block rule: https://developers.cloudflare.com/waf/tools/user-agent-blocking/#create-a-user-agent-blocking-rule
- add a middleware in your app (if you're using Pages) to block by User Agent: https://developers.cloudflare.com/pages/functions/middleware/
- use a worker to block as well, here's some example code:
export default {
async fetch(request, env, ctx) {
const userAgent = request.headers.get('user-agent')
const toRedirect = userAgent.includes('MSIE 8.0')
if (toRedirect) {
const destinationURL = "https://example.com";
const statusCode = 301;
return Response.redirect(destinationURL, statusCode);
}
return fetch(request);
},
};
Hope this helps!
Gualter
Rationale
From my quick product search, this seems to be the right approach. The WAF feature is not available to free accounts so I offered alternative paths.