Do a time based redirect in cloudflare workers?

Hi there,
I am trying to redirect my visitor to the specific website at a specific time.
For Example-
12 AM to 12 PM - app1 ( has its own URL)
12 PM to 12 AM - app2 (also has its own URL).
Actually, I was trying to do load balancing through workers but it didn’t succeed. Can anyone help me to load balance them and attach HOST header to them.

If above is not possible, then please suggest a solution for time-based forwarding.
Seeking forward for help.

Hey @aayush23467 - interesting problem! I worked up a little example, making use of the Date class in JS to fetch from two different URLs based on the current hour. Note that it’s currently set to the timezone “America/New_York”, so you’ll need to adjust accordingly.

Here’s the preview code in our Workers playground!

2 Likes

First of all thanks for such a prompt response. Its working perfect. But I have a question can i do not set host header to an external service such as a herokuapp and server it through this example. Once again thanks for looking into my problem.

Unfortunately, we don’t allow rewriting the Host header for security reasons – more detail in this thread:

1 Like

Hi there,
I worked on your example and its working perfectly fine, But I am not able to go to a subset of URL. For example-
if one visits a.com/ping.php then it should redirect it to appropiate server on time bases with that /ping.php

Thanking you in Advance.