5

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.

1 Answer 1

2

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.

3
  • 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
    Commented Mar 22, 2012 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
    Commented Jul 9, 2012 at 9:21
  • I was strugling with this problem too! Whenever I went in Settings > Permalink and Saved, my custom rewrite rules were removed. Adding theme also at init time solved the problem!
    – Dalen
    Commented May 5, 2015 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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