0

I made a simple redirection plugin that works like this:

add_action('wp_head', 'myredir');
function myredir(){
    if ( is_404() ) {
        $Path=$_SERVER['REQUEST_URI'];
        wp_redirect( 'http://old.example.com'.$Path );
        exit();
    }
}

The goal is redirect 404 errors to the old domain (I need to do a manual migration from an old website and it's going to take ages)

On Apache it works fine, on Nginx it doesn't work. What can I do to fix my plugin? The Redirection plugin works fine and it looks like is using wp_redirect, so there something wrong with my code

3
  • This is not related to nginx at all. The latest hook on the front end for HTTP headers is template_include.
    – fuxia
    Dec 9, 2016 at 12:09
  • using hook for template_include worked perfectly. So this is a change in Wordpress 4.7 and not because of nginx? Because I have another install on apache+wordpress 4.6.1 where my code works fine Dec 9, 2016 at 12:20
  • No, this isn't new. wp_head was always too late as Mark pointed out.
    – fuxia
    Dec 9, 2016 at 12:23

1 Answer 1

2

It should not have worked on apache as well. wp_head is too late to make a redirect as there is already some output sent at that point and php will not send the http headers required for the redirection. You need to hook on wp_loaded or any other hook before output actually happens.

2
  • I hooked on get_header and it worked fine Dec 9, 2016 at 12:56
  • 1
    +1, it is just such an esoteric hook, I decided not to mention it, maybe I should had. Dec 9, 2016 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.