Redirection done better, with CloudFlare Workers

Creating a redirect is in itself not hard. A redirect based on a mix of certain conditions, now that can get tedious. Often, you end up dealing with running apps for nothing but redirecting. Does it have to be that way?

No, with CloudFlare Workers you have an easy to use and quite powerful tool at hands to do the job — without any infra management at all. And you get 100k free API calls per day. Not to shabby and that’s why I want to show you how to do use CloudFlare Workers for your redirection needs.

A simple HTTP endpoint using CloudFlare Workers

Creating a simple HTTP endpoint is super simple and fast to do, e.g. with a JS like this:

Date-based re-directs

Now let us imagine you want to have a URL redirect that does some validation, eg some basic date checking.

And the CloudFlare “Quick Edit” UI even features some primitive intellisense.

The catch here being that a default timezone is used.

Here’s how you’d deal with different timezones:

You could do way more than that with these Workers:

In fact, there are plenty more examples of what you could do:

Bypassing the Cache

CloudFlare workers aggressively cache everything.

This is important for example for the cases when you fetch data from external URIs within your cloud worker. Some tips here:

Beware, though! The Cloud Workers limits in the unpaid version of CloudFlare Workers do not suggest you should fetch data from external resources but rather stick to very simple use cases like redirection, changing response headers and such.