permalink and add_rewrite_rule – error 404

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.