Question 6

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?

Step 1:  Great question and yes, there is in fact another way! What we'll do is create a Worker which will help you do just that. First go to your Cloudflare dashboard and click on (Workers → Create Service / Create Worker). Create a worker file (e.g., src/index.js).

[I want them to use Workers so that they can successfully fix their problem. Since Workers allow for customizable solutions such as redirecting people with specific internet browsers, it will be a great solution.

Step 2: Create a worker file (e.g., src/worker.js)

Now what we will need to do, is within your JavaScript code, go ahead and paste this code into your worker.js file:

addEventListener("fetch", event => {
 const req = event.request;
 const ua = req.headers.get("user-agent") || "";
 if (ua.includes("MSIE 8.0")) {
   event.respondWith(Response.redirect("https://example.com/ie8-page", 302));
   return;
 }
 event.respondWith(fetch(req));
});

[I chose to use an Event Listener because it requires less steps and still gets the job done. It is also simpler in my opinion.]

Now when we run it, we can test to see if it will work. Since you already have MSI 8.0 downloaded, how about you test that. Great! It works! Now you should be all set. Is there anything else I could help you out with? Alright, have a wonderful rest of the day!

[I want them to get in the habit of testing it themselves.]

Tools Used:

Documentation from: https://developers.cloudflare.com/workers/examples/redirect/ in order to find similar redirect code for the issue.