User documentation
  1. What is redirection.io?
  2. Starter's guide
  3. What are organizations and projects?
  4. Invite new collaborators
  5. User account and preferences
  6. Using traffic logs
  7. Create a rule
  8. Triggers and markers reference
  9. Actions reference
  10. How to bulk-import or export redirection rules?
  11. Managing instances
  12. Project notifications
  13. Project segmentation
  14. How much does it cost?
  15. Can I use redirection.io for free?
  16. About us

Developer documentation
  1. TL;DR; Fast track
  2. Installation of the agent
  3. Upgrading the agent
  4. Agent configuration reference
  5. Available integrations
  6. The agent as a reverse proxy
  7. nginx module
  8. Apache module
  9. platform.sh integration
  10. Cloudflare Workers integration
  11. Fastly Compute@Edge integration
  12. Vercel Middleware Integration
  13. Using redirection.io with Docker
  14. How fast is it?
  15. Public API

Crawler
  1. What is the redirection.io crawler?
  2. Start a crawl
  3. Schedule a crawl
  4. Analyzing the results of a crawl
  5. The crawls list
  6. Crawl credits and pricing
  7. Crawl Errors
  8. Crawler metrics reference
  9. Crawler columns reference

Managed instances
  1. What are managed instances?
  2. Add a domain to your project
  3. Managed instances limits and quota
  4. Frequently asked questions

Knowledge base
  1. Create your first redirections
  2. redirection.io rules cookbook
  3. Setting up a redirection server on Azure Cloud
  4. Structured data and Rich Snippets
  5. What is a URL redirection?
  6. Why use URL redirections and how to setup

Legacy versions
  1. Agent 1.x configuration reference
  2. Legacy integrations
  3. Legacy Cloudflare Workers integration

Changelogs
  1. redirectionio-agent
  2. libnginx-mod-redirectionio
  3. libapache2-mod-redirectionio

Legacy Cloudflare Workers integration

This documentation page is about the legacy version of our Cloudflare Workers integration and is kept for archive purpose. In particular, this legacy version cannot run the advanced triggers and actions, nor the ruleset settings. We strongly advise you to rather use the current version of our Cloudflare Worker integration.

This version has been deprecated on September 2nd, 2020

In order to use the legacy integration of redirection.io with Cloudflare Workers:

  1. create a redirection.io account;
  2. create a redirection.io organization and a project. At this step, you may want to invite your co-workers ;
  3. head to the "instances" screen of your project, and locate your "project key" by clicking on "Setup on your infrastructure"
  4. Cloudflare Workers menulogin to your Cloudflare account, then click the "Workers" menu and hit the "launch editor" button ;
  5. Cloudflare Workers editorin the left part of the editor, paste our redirection.io Cloudflare Worker code, and replace the project token with your own value ;
  6. hit the "Save" button at the bottom of the editor ;
  7. Cloudflare Workers route tabin the "Routes" tab, enable this Cloudflare Worker for your whole website.

With all these steps completed, you should start getting traffic logs in your redirection.io's manager. You're all set :-)

legacy redirection.io Cloudflare Worker code

This documentation section is about the legacy version of our Cloudflare Workers integration and is kept for archive purpose. In particular, this legacy version cannot run the advanced triggers and actions. We strongly advise you to rather use the current version of our Cloudflare Worker integration.

addEventListener('fetch', event => {
  event.respondWith(redirectAndLog(event.request))
})

const options = {
  token: 'PASTE YOUR PROJECT TOKEN HERE',
  timeout: 2000,
}

async function redirectAndLog(request) {
  const [response, ruleId] = await redirectOrPass(request)
  log(request, response, ruleId)

  return response
}

async function redirectOrPass(request) {
  const urlObject = new URL(request.url)
  const context = {
    host: urlObject.host,
    request_uri: urlObject.pathname + urlObject.search,
    user_agent: request.headers.get('user-agent'),
    referer: request.headers.get('referer'),
    scheme: urlObject.protocol.includes('https') ? 'https' : 'http',
    use_json: true,
  }

  let response

  try {
    response = await Promise.race([
      fetch('https://proxy.redirection.io/' + options.token + '/get', {
        method: 'POST',
        body: JSON.stringify(context),
        headers: {
          'User-Agent': 'cloudflare-service-worker/0.0.1'
        },
      }),
      new Promise((_, reject) =>
        setTimeout(() => reject(new Error('Timeout')), options.timeout)
      ),
    ])
  } catch (error) {
    return [await fetch(request), null]
  }

  const data = await response.text()

  try {
    response = JSON.parse(data)
  } catch (error) {
    // If some errors play regular request, anyway request will be in error when no redirection (404)
    return [await fetch(request), null]
  }

  // Send gone response
  if (response.status_code === 410) {
    return [new Response('', { status: 410 }), response.matched_rule.id]
  }

  // Send redirection response
  return [new Response('', {
    status: Number(response.status_code),
    headers: {
      Location: response.location,
    },
  }),
  response.matched_rule.id]
}

async function log(request, response, ruleId) {
  const urlObject = new URL(request.url)
  const context = {
    status_code: response.status,
    host: urlObject.host,
    method: request.method,
    request_uri: urlObject.pathname + urlObject.search,
    user_agent: request.headers.get('user-agent'),
    referer: request.headers.get('referer'),
    scheme: urlObject.protocol.includes('https') ? 'https' : 'http',
    use_json: true,
  }

  if (response.headers.get('Location')) {
    context.target = response.headers.get('Location');
  }

  if (ruleId) {
    context.rule_id = ruleId;
  }

  try {
    return await fetch(
      'https://proxy.redirection.io/' + options.token + '/log',
      {
        method: 'POST',
        body: JSON.stringify(context),
        headers: {
          'User-Agent': 'cloudflare-service-worker/0.0.1'
        },
      }
    )
  } catch (error) {
    // Do nothing, do not matters if some logs are in errors
    console.log('could not log')
    console.log(error)
  }
}
This page has been updated on Aug 28, 2025
Can't find your answer?