How to do an inexpensive redirect via a Wordpress plugin?

143 views Asked by At

My customer has one of the slowest Wordpress sites I have ever seen, with a request taking up to 6 seconds to start producing contents. My guess is that something in Wordpress is taking a lot of time.

We want to set up an alternative, experimental setup that should be faster and redirect part of his traffic to this alternative setup using alt-svc headers. We will set those headers with a Wordpress/PHP plugin, because we don't have access to the NginX server config in the hosting company.

The problem is that a regular request is taking 6 seconds inside the Wordpress processing loop. We would like to avoid those delays in some cases by doing something like this when handling a request to /mypage (in pseudo code):

 if cookie-went-through-here not set and browser is good:
         set cookie-went-through-here
         redirect to this same url adding a '?same' query string ...
             .. and alt-svc header to faster setup
         wait half a second
         exit wordpress processing and send the (relatively) ...
             .. empty HTTP body of a redirect.
 else:
         Just keep going (and let the 6 seconds wait happen).

The effect of the block above is that browsers will be redirected from /mypage to /mypage?same (to avoid redirect loops). But the alt-svc header will be interpreted after the first request, and if things go all right the second request will hopefully go to the alternative, faster setup.

So, here are my questions:

  • Which of the Wordpress hooks can I use for the logic above? In particular, I would like to use a hook that is executed as early as possible, before whichever plugin is causing the 6 seconds delay.
  • I'm new to Wordpress, is there a way to do this even before plugins are loaded?
0

There are 0 answers