WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Adding rewrite rules and $wp_rewrite->flush_rules() (9 posts)

  1. microkid
    Member
    Posted 4 years ago #

    Hi,

    I've been struggling with this for a while, and the documentation on the rewrite API is a little scarce. So I hope someone's out there who can shed some light on this.

    If I add rewrite rules, do I have to flush the rules on every page load? It seems that if I don't flush them, it will keep working for a short while. Then after a couple of minutes it will break.

    Flushing the rules currently causes an extra 100 (!) mysql queries on each page load. The queries look like this:

    [23] => Array
            (
                [0] => SELECT * FROM wp_posts WHERE ID = 5106 LIMIT 1
                [1] => 0.000135898590088
                [2] => require, wp, WP->main, do_action_ref_array, call_user_func_array, XIC_flush, WP_Rewrite->flush_rules, WP_Rewrite->wp_rewrite_rules, WP_Rewrite->rewrite_rules, WP_Rewrite->page_rewrite_rules, WP_Rewrite->page_uri_index, get_page_uri, get_page, get_post
            )
    
        [24] => Array
            (
                [0] => SELECT <code>post_parent</code> FROM wp_posts WHERE ID = 5068 LIMIT 1
                [1] => 0.0001220703125
                [2] => require, wp, WP->main, do_action_ref_array, call_user_func_array, XIC_flush, WP_Rewrite->flush_rules, WP_Rewrite->wp_rewrite_rules, WP_Rewrite->rewrite_rules, WP_Rewrite->page_rewrite_rules, WP_Rewrite->page_uri_index, get_page_uri, get_page, get_post, _get_post_ancestors
            )
    
        [25] => Array
            (
                [0] => SELECT ID, post_name, post_parent FROM wp_posts WHERE post_type = 'attachment' AND post_parent = 5106
                [1] => 0.000179052352905
                [2] => require, wp, WP->main, do_action_ref_array, call_user_func_array, XIC_flush, WP_Rewrite->flush_rules, WP_Rewrite->wp_rewrite_rules, WP_Rewrite->rewrite_rules, WP_Rewrite->page_rewrite_rules, WP_Rewrite->page_uri_index
            )

    They are "small" queries, but still I'd love to get rid of them..

  2. microkid
    Member
    Posted 4 years ago #

    *bump*

  3. terryjsmith
    Member
    Posted 4 years ago #

    While I do not have a solution yet, I have spent all day (and the last week) trying to figure out the same issue with one of the WordPress core developers.

    Our issue at least is in using the object cache. We have set ours up for a default timeout of 5 minutes, which is the same amount of time the URLs seemed to be "good" for.

    What happening is that there is no hook available for when WP is generating the rewrite rules. This is happening before the 'init' action and at most other actions, the $wp_rewrite variable has not been initialized yet (even if you initialize it, it generates the rules right away).

    The developer I spoke with is looking into adding another hook into the code, or possibly getting rid of the rewrite rules being transient (as of 2.8) all together.

    I hope this helps; I am still searching for a temporary fix and will let you know if I find one.

    Terry

  4. microkid
    Member
    Posted 4 years ago #

    Actually, I did come up with a solution.

    There's a filter called when the rewrite rules are created: rewrite_rules_array

    Using this seems to work and does NOT time out:

    function my_rewrite_rules( $rewrite_rules ) {
    
        $new_rules = array( 'foobar/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?foobar=$matches[1]&paged=$matches[2]' );
    
        $rewrite_rules = $new_rules + $rewrite_rules;
    
        return $rewrite_rules;
    
    }
    
    add_filter('rewrite_rules_array', 'my_rewrite_rules');

    Note that you will need to flush the rewrite rules once. This can easily be done by going to the options/permalinks page in your wp-admin.

    Sorry for not letting you know earlier. Please let me know if this will work for you as well.

  5. terryjsmith
    Member
    Posted 4 years ago #

    Wow, nicely done. Unfortunately I was trying to add an endpoint so the rewrite rules become several degrees more complicated. The functionality is actually being reversed in WordPress so the original implementation will soon work as well. We're testing a patch on the WP core.

    Thanks for posting a solution though, that's awesome that you found a workaround :)

    Terry

  6. Mark Jaquith
    WordPress Lead Dev
    Posted 4 years ago #

    This is a bug in the way we store rewrite_rules in "transients" instead of options like we used to. I think our fix for WP 2.9 is going to be to revert to the old method.

    http://core.trac.wordpress.org/ticket/10981

    In more detail, people using transient object cache backends like Memcache or APC can expect their rewrite_rules transient to... well... transit (go away). When this happens, WP dutifully regenerates the rules, but it does so before any plugin would have added its endpoint (and indeed there wasn't an appropriate hook for the plugin to add it such that WP would be aware of it in this case). So the rules get saved without your endpoint changes. This will be fixed for WP 2.9!

  7. microkid
    Member
    Posted 4 years ago #

    That's amazing, I've been struggling with this for over a year!

    Please let me know if you need me for testing, I've been using custom rewrites a lot.

    Thanks guys.

  8. bennow
    Member
    Posted 4 years ago #

    Maybe this is the issue we're running into.....

    We're using memcache for pagecaching with W3 Total Cache, with WP mu.... We have one set that gets about 15mm pageviews a month, and we're trying to get it on the WP platform. When we start moving traffic over, we get a backup in the wp_options table of hundres of _transient_rewrite_rules records backing up... all of which are just duplicate records. IT eventually just hoses our database because on pagerequests that table grows to 10-15 megs.

    We disabled memcache on pagecaching, and went with disk caching instead. Hopefully this solves the problem. If we roll back our files and make the changes in trac, would that solve the issue we're experiencing?

  9. Frederick Townes
    Member
    Posted 4 years ago #

    @bennow, in general unless you have multiple servers in your configuration the use of memcache + memcached is not necessary. If you're indeed using the latest version of W3 Total Cache, I recommend disk enhanced mode for page cache, and APC (or memcached + memcache if APC is not available) for database and again disk for minify caching.

Topic Closed

This topic has been closed to new replies.

About this Topic