WordPress.org

Support

Support » Themes and Templates » wp_redirect with 404 status returns white page

wp_redirect with 404 status returns white page

  • Hi you guys,

    I put the following code in the 404.php of my theme:

    <?php
    wp_redirect( "http://www.myredirecturl.com", 404 );
    exit;

    WordPress returns a white screen in this case.
    If I use another status code like 301 or 302, the problem doesn’t exist.

    I’m aware that I can define ErrorDocuments in my .htaccess, but would like to let my WordPress multisite installation handle this itself.
    (Btw: I also tried this with a WP single blog installation – same problem here)

Viewing 7 replies - 1 through 7 (of 7 total)
  • I have the exact same problem.
    This works however since my theme has a 404.php page:
    wp_redirect(home_url('404.php'), 302); exit;

    But i want to set http-status to 404, like so:
    wp_redirect(home_url('404.php'), 404); exit;

    What am i missing here?

    I just solved it by using $wp_query:

    if(isset($imported_post)){
            wp_redirect($post_link, 301); exit;
        }
        else{ // No match, generate 404
            global $wp_query;
            $wp_query->is_404 = true;

    Hope this helps!

    Chip Bennett

    @chipbennett

    Theme Review Admin

    I’m not terribly familiar with the wp_redirect() function, but at first glance, it appears that by passing it the 404 error code, you’re sending it into a recursive loop: if 404, redirect to some URL, with status “404” – which the server will recognize as a 404, and cause it to invoke the WordPress 404 template – which redirects to some URL, with the status “404”…

    Question: if it works when you pass a 301 or 302 status, why not use one of those? Why are you trying to pass it a 404 error?

    Thanks for your reply Chip!
    Good to get your input about the recursive loop!
    I’m currently working with migrating a large website to WordPress. This means importing lots of posts which will all get new URIs. This site has been around for awhile and there are probably a lot of old bookmarks out there. I need to pass any request containing a broken links to a 404 (since the post doesn’t exist in these cases), also Google will index broken links incorrectly if these will be 301 or 302.
    But $wp_query->is_404 = true; does the job just fine for me.

    Chip Bennett

    @chipbennett

    Theme Review Admin

    the wp_redirect() function is expecting a redirect status (i.e. “301” or “302”).

    The “404” status has already been passed, otherwise you’d never have your 404.php page served, and wp_redirect() would never get called.

    True.

    Thanks to all of you for the answers,
    now I’m a little bit wiser 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘wp_redirect with 404 status returns white page’ is closed to new replies.