Redirecting bot traffic with Cloudflare helps you serve optimized, prerendered content to search engines and social media bots, improving your site’s SEO and performance. This guide shows how to set up a Cloudflare Worker to detect bots and redirect them to Page Replica’s cache, while letting regular users and static file requests pass through normally.
Why do this?
Scope:
bot-redirect-worker).YOUR_SITE_ID with your actual Site ID):/**
* @typedef {Object} Env
*/
export default {
/**
* @param {Request} request
* @param {Env} env
* @param {ExecutionContext} ctx
* @returns {Promise<Response>}
*/
async fetch(request, env, ctx) {
const SITE_ID = "YOUR_SITE_ID"; // TODO: Replace with your Site ID
// List of common bot user agents
const botUserAgents = [
// Search Engine Bots
"googlebot",
"bingbot",
"slurp",
"duckduckbot",
"baiduspider",
"yandexbot",
"sogou",
"exabot",
"facebot",
"ia_archiver",
// Google Bots
"googleother",
"google-inspectiontool",
"mediapartners-google",
"adsbot-google-mobile",
"adsbot-google",
"google-sitemaps",
"google-favicon",
"google-image",
// Bing Bots
"bingpreview",
// Advertising Bots
"adsbot-google",
"propellerads",
"mediapartners-google",
// Social Media Bots
"twitterbot",
"linkedinbot",
"pinterestbot",
"telegram",
// Other Bots
"applebot",
"semrushbot",
"mj12bot",
"dotbot",
"ahrefsbot",
"rogerbot",
"linkdexbot",
"uptimerobot",
"ltx71",
"archive.org_bot",
];
// List of common media file extensions
const mediaExtensions = [
".jpg",
".jpeg",
".png",
".gif",
".bmp",
".svg",
".webp",
".ico",
".tiff",
".tif",
".mp4",
".mp3",
".wav",
".avi",
".mov",
".mkv",
".flv",
".wmv",
".css",
".xml",
".json",
];
const url = new URL(request.url);
const isMediaFile = mediaExtensions.some((ext) =>
url.pathname.toLowerCase().endsWith(ext),
);
// If it's a media file, serve the request as is
if (isMediaFile) {
return fetch(request);
}
// Check if the User-Agent header contains any bot user agent strings
const userAgent = request.headers.get("User-Agent") || "";
const isBot = botUserAgents.some((bot) =>
userAgent.toLowerCase().includes(bot),
);
// If the request is from a bot, fetch the content from another URL and return it
if (isBot) {
const domainName = url.hostname;
const targetUrl = `https://cache.page-replica.com/${SITE_ID}/${domainName}${url.pathname}`;
const response = await fetch(targetUrl);
const contentType = response.headers.get("Content-Type");
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: {
"Content-Type": contentType,
},
});
}
// For all other requests, serve the request as is
return fetch(request);
},
};
example.com/*).YOUR_SITE_ID?
Make sure to use your actual Site ID from the Page Replica dashboard.example.com/*) and that the worker is deployed to the correct domain.mediaExtensions array.You can test if the worker is redirecting bots by using curl with a bot user-agent:
curl -A "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" https://yourdomain.com/page
You should see a response from the Page Replica cache if everything is set up correctly.
And that’s it! Your Cloudflare Worker is now set up to redirect bot traffic to prerendered content from Page Replica.