Redirecting a few thousand URLs using PHP

I am migrating a site which will require about 5000 redirects, in a format such as redirects to

Normally I would accomplish this through .htaccess or an nginx module. However I’m on a WordPress specific host, Pantheon, which does not allow access to that.

Therefore the only solution I could think of is to use PHP. The following is working. There’s some WordPress specific code in there to prevent WordPress from just throwing a 404.

add_filter('template_redirect', 'my_404_override');
function my_404_override()
  global $wp_query;
  if (strpos($_SERVER['REQUEST_URI'], 'issues') !== false) {
    $redirectURL = "/resources";
    if (strpos($_SERVER['REQUEST_URI'], '123') !== false) {
      $redirectURL .= "/articles/why-acme-is-great/";

  if (!empty($redirectURL)) {
    $wp_query->is_404 = false;
    header('HTTP/1.0 301 Moved Permanently');
    header('Location:' . $redirectURL);

This works fine. However I have two concerns:

  1. With a list of 5000, what kind of impact will this have on performance? I’m planning on using some larger conditionals and then narrowing down (in the example above, I first check for /resources before looking at specific IDs.
  2. While this list will in theory never need to be modified, it feels like an ugly solution both in terms of syntax and logic. Is there a better method I’m not thinking of?

Read more here: Redirecting a few thousand URLs using PHP

Leave a Reply

Your email address will not be published. Required fields are marked *