2

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).

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

3

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.

  • Can't thank you enough for this. I was creating custom themes with an MVC framework bolt-on as well, and things worked in the browser, but not over AJAX or command line (with wget command test on Linux). Found I had to add "header('HTTP/1.1 200 OK');" in my index.php of the MVC framework in the theme in order to make it work. Glad that you knew about this bug or I'd be pretty frustrated for weeks! – Volomike Dec 13 '11 at 6:06
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy