Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've written some custom rewrite rules for a special feature on http://www.pewhispanic.org/states/. States is a page with a custom page template, page-states.php. The logic for returning the right data based on the request is done there.

I created custom rewrite rules so I could have a pretty urls with custom parameters that get sent to page-states.php. The following code is in my themes functions.php file:

//Rewrites for the States and Counties Database
function filter_statedb_rewrite_rules( $wp_rewrite ) { //Lets us have URLs like /states/state/md/ etc.
    $statedb_rewrite_rules = array(
        "states/counties/([a-zA-z]{2})/?$" => "index.php?pagename=states&counties=" . $wp_rewrite->preg_index(1),
        "states/county/(\d+)/?$" => "index.php?pagename=states&countyid=" . $wp_rewrite->preg_index(1),
        "states/state/([a-zA-z]{2})/?$" => "index.php?pagename=states&stateid=" . $wp_rewrite->preg_index(1)
    );
    $wp_rewrite->rules = $statedb_rewrite_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'filter_statedb_rewrite_rules');


function kill_statesdb_canonical_redirect($redirect_url, $requested_url) {
    global $wp;

    $statedb_rewrite_rules = array(
        "states/counties/([a-zA-z]{2})/?$",
        "states/county/(\d+)/?$",
        "states/state/([a-zA-z]{2})/?$"
    );

    if( in_array($wp->matched_rule, $statedb_rewrite_rules) ) { 
        return false;
    } else {
        return $redirect_url;
    }
}
add_filter('redirect_canonical', 'kill_statesdb_canonical_redirect', 10, 2);


function add_statedb_query_vars($query_vars) {
    $query_vars[] = 'counties';
    $query_vars[] = 'countyid';
    $query_vars[] = 'stateid';

    return $query_vars;
}
add_filter( 'query_vars', 'add_statedb_query_vars' );

For some reason the rewrite rules fail to stick sometimes (I hate when things only work sometimes). I visit Settings -> Permalinks to flush the rewrite rules and everything is working great. A couple days go by and then I get an email saying pages like http://www.pewhispanic.org/states/state/md/ are returning a 404. Crap. I go and flush the rewrite rules and everything is fine again.

What am I missing here? Why are my custom rewrite rules sometimes not being applied? Is there a better action than generate_rewrite_rules to hook on to? Do I even need to mess with the redirect_canonical filter? I don't think it's doing anything.

Any insight would be most appreciated.

share|improve this question

1 Answer 1

I am not completely sure why this happens but I have seen it too. In my plugin Opera Speed Dial Preview I had to add the rewrite rules on init too – that fixed it.

Sometimes other plugins or later visits to the permalink page may reset your custom roles … reminds me to write a logging plugin for this as WordPress doesn’t do this natively.

share|improve this answer
    
Good thing there is an example on the Rewrite Class codex page for how to check and automatically flush the rewrite rules if specific ones aren't found. codex.wordpress.org/Class_Reference/WP_Rewrite#Examples I added this check in and I'll see if it fixes the problem. Weird since similar code is on other sites and works just fine. –  kingkool68 Mar 22 '12 at 15:17
    
Thanks @RussellHeimlich for the link to the example to check for the rule during wp_load. I was experiencing the same thing as you –  CommentLuv Jul 9 '12 at 9:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.