Question 6 of 6
Redirection based on user agent
Customer ticket
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?
1. Email reply to the customer
Hi there,
Thanks for your question. You’re right that Page Rules can only match on the URL, not on request headers like the User-Agent. (Page Rules are also being phased out in favour of the newer Rules products.) The good news is that this is straightforward with Redirect Rules, which are available on every plan, including Free. I’ve also included a Workers option in case you need more logic.
Option 1 (recommended): a Single Redirect rule
In the dashboard, go to Rules > Overview > Create rule > Redirect Rule and set:
- Rule name: Redirect IE8 users
- When incoming requests match: Custom filter expression, then select Edit expression and enter:
(http.user_agent contains "MSIE 8.0" and http.request.uri.path ne "/legacy-browser.html")- Then: Static URL redirect, target
https://example.com/legacy-browser.html - Status code:
302(temporary). I recommend 302 rather than 301 because the decision depends on the browser, and browsers cache 301s permanently. - Preserve query string: as you prefer.
The second condition in the expression stops a redirect loop on the target page. Then select Deploy. A few notes:
- The hostname must be proxied (orange cloud) for the rule to run.
containsis case-sensitive and matches a substring anywhere in the header, so you don’t need the*wildcards from your example. To match several versions, combine conditions withor, e.g.http.user_agent contains "MSIE 7.0" or http.user_agent contains "MSIE 8.0". Regular-expression matching (matches) is available on Business and Enterprise plans.- Rule quotas: Free 10, Pro 25, Business 50, Enterprise 300 Single Redirect rules per zone.
- Redirect rules are evaluated before the cache, so cached pages won’t stop IE8 visitors from being redirected.
You can test it without an IE8 machine by sending that user agent with curl:
curl -sI -A "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)" https://example.com/
# HTTP/2 302
# location: https://example.com/legacy-browser.html
curl -sI https://example.com/ # normal browsers: 200, no redirectOption 2: a Cloudflare Worker (more flexible)
If you need more complex logic (for example several browsers sent to different pages, A/B testing, or reading the redirect map from KV), a Worker gives you full control. Workers are available on the Free plan (100,000 requests/day) and on Workers Paid. For lightweight cases on Pro and above, Cloudflare Snippets run the same kind of JavaScript without a separate Worker.
// src/index.ts: redirect Internet Explorer 8 visitors to a legacy page
export default {
async fetch(request: Request): Promise<Response> {
const url = new URL(request.url);
const ua = request.headers.get("User-Agent") || "";
const target = "/legacy-browser.html";
// Match IE 8 and avoid a redirect loop on the target page itself
if (ua.includes("MSIE 8.0") && url.pathname !== target) {
const destination = new URL(target, url.origin);
return Response.redirect(destination.toString(), 302);
}
// Everyone else: pass the request through to the origin unchanged
return fetch(request);
},
};// wrangler.jsonc
{
"name": "ua-redirect",
"main": "src/index.ts",
"compatibility_date": "2026-10-01",
"routes": [
{ "pattern": "example.com/*", "zone_name": "example.com" },
{ "pattern": "www.example.com/*", "zone_name": "example.com" }
]
}Deploy with npx wrangler deploy, or paste the code into the dashboard editor and add the routes under Settings > Domains & Routes. Note that a Worker on example.com/* runs on every request, so for a simple redirect like this one, Option 1 is cheaper and simpler.
Let me know which approach you go with. I’m happy to review the rule or the code with you.
Kind regards,
Marwan
Cloudflare Customer Support
2. Thought process
The customer has correctly found that Page Rules can’t do this. Page Rules only match URL patterns, and they are now marked as deprecated. So I confirm that first, then offer the simplest correct tool followed by a more powerful fallback:
- Single Redirects support custom filter expressions on any request field, including
http.user_agent. I checked availability and quotas in the current docs: available on all plans (Free 10, Pro 25, Business 50, Enterprise 300 rules) with regex on Business and above, so I can recommend it with confidence even on Free. - Pitfalls I anticipated: redirect loops (exclude the target path), 301 caching in browsers (use 302 for client-dependent redirects), the proxied-record requirement, and the customer’s
*MSIE 8.0*glob syntax (withcontainsthe wildcards are unnecessary). - Workers for anything beyond a simple match. I included complete, deployable code with routes, because the role is Developer Platform focused and customers like this often grow into Workers. I also mention Snippets (Pro+) as a middle ground and point out the cost trade-off.
- Verification: a
curl -Atest lets the customer prove the behaviour without finding an IE8 machine.
3. Tools I would use, and why
| Tool | Why |
|---|---|
Dashboard: Rules > Redirect Rules | Native, no-code way to redirect on the User-Agent, on every plan. |
Rules language docs (http.user_agent, contains, matches) | Confirm field names, operator semantics and plan-gated regex support. |
curl -sI -A "…MSIE 8.0…" | Simulate an IE8 visitor and confirm the 302 and Location header; compare with a normal user agent. |
Cloudflare Trace (Rules > Trace) | Check which rule matches a request with a given User-Agent. |
Wrangler (npx wrangler deploy / tail) | Deploy the Worker alternative with routes, then watch live requests to confirm it fires. |
Workers Playground / dashboard editor | Quickly prototype and test the Worker logic before attaching routes. |