$error = 404 after caught add_rewrite_rule

I have a rewrite rule as this to caught old urls from a previous version of the site:

add_rewrite_rule( '(ninos|familia|hogar|mujeres|hombres)-[a-z-]+\.php', 'old_id_redirect.php', 'top' );

The rewrite rule works more or less as intended, and I see in the debugger that the request goes through old_id_redirect.php.

In there, there is only (so far) a call to wp-blog-header.php, and nothing and almost nothing else.

I stop execution just after that there to see the enviroment state, and I see that $error is set to “404”.

Why is that? If the add_rewrite_rule was caught and the redirection made, why is $error is set to 404?

1 Answer
1

I had the same issue and found a solution. From what I learned so far, there might be two causes to this problem:

  1. WP doesn’t recognize your rewrite rules because they are not cached yet. You can check this by dumping get_option( 'rewrite_rules' ); and if your rules did get cached, then they’ll be in that option. To ensure your rewrite rules are cached, make sure you call:

    global $wp_rewrite; 
    $wp_rewrite->flush_rules(); 
    
  2. Your server configuration is not properly set to use .htaccess files (for Apache servers). An easy way to check is to type a URL address that doesn’t exist after your WordPress host, e.g. http://example.com/location-that-doesnt-exist. If your server is properly configured to use .htaccess files, then you’ll see a 404 page styled by your WP theme. If it is NOT properly set, then you will see the server’s default 404 page and it will not be styled by your WP theme.

Leave a Comment