Skip to content

Create a Cloudflare conditional redirection

This tutorial explains how to create a worker in Cloudflare to handle conditional redirects to the original RSS feed while maintaining access to the original source by the FeedPress bot.

Don't forget to replace [[URL]] with your FeedPress URL (for instance https://feedpress.me/mywebsite

Step 1: Create the Worker Script

  1. In your Cloudflare dashboard, go to Workers and click Create a Worker.
  2. Replace the default code with the following:
addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const userAgent = request.headers.get('user-agent') || ''

  // Redirect if User-Agent does not contain "FeedPress"
  if (!userAgent.includes('FeedPress')) {
    return Response.redirect('[[URL]]', 302)
  }

  // If the conditions aren't met, proceed to original URL
  return fetch(request)
}
  1. Save and deploy the Worker.

Step 2: Apply the Worker to your Path

  1. Go back to the Workers dashboard, find the Worker you just created, and click Add Route.
  2. Specify the route as your current original feed URL, for example mywebsite.com/rss .
  3. Select the Worker you just deployed from the dropdown list, and save.

Explanation

  • If the User-Agent does not contain FeedPress, the Worker redirects the user to your FeedPress feed with a 302 status.
  • If the User-Agent contains FeedPress, the Worker allows the request to proceed without redirection.