Custom rewrite rule serves content, but returns 404 error code

I have a plugin that sets up a custom rewrite rule. I can access the short URL and the content is returned correctly, but the HTTP response is 404, Page not found.

From searching the web it seems like WordPress is returning 404, because it thinks this is not a valid WP page (http://wordpress.org/support/topic/404-on-custom-non-wordpress-pages).

Read More

Is there a recommended solution to this problem? How can I get WP to return a normal status code for a rewritten URL?

global $wp_rewrite;
add_rewrite_rule('list-data$', 
                 '/wp-content/plugins/data-lister/list-data.php', 'top');

Thanks!
Mark

Related posts

Leave a Reply

2 comments

  1. This has been an issue in core for a while, http://core.trac.wordpress.org/ticket/10722.

    The simplest solution is to just overwrite the headers on ‘template_redirect’. You can replace them as long as you haven’t started any output yet, which you shouldn’t have at this point. Just call status_header( 200 );

    No cache headers are also sent when the WP Class sends the 404 headers, so you’ll probably want to replace those with information based on the content you’re pulling from that PHP page.

  2. Add this to functions.php:

    function fix_rewrite_404_header()
    {
        if( is_page(124) )  // or whatever condition you want to use
        {
            status_header(200);
        }
    }
    add_action( 'pre_get_posts', 'fix_rewrite_404_header');
    

    I tried ‘template_redirect’ action (as @prettyboymp suggested) but it didn’t work.