Appearance
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 instancehttps://feedpress.me/mywebsite
Step 1: Create the Worker Script
- In your Cloudflare dashboard, go to Workers and click Create a Worker.
- 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)
}
- Save and deploy the Worker.
Step 2: Apply the Worker to your Path
- Go back to the Workers dashboard, find the Worker you just created, and click Add Route.
- Specify the route as your current original feed URL, for example
mywebsite.com/rss
. - 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.