我已经为上的一个特殊功能编写了一些自定义重写规则http://www.pewhispanic.org/states/. “状态”是一个具有自定义页面模板“页面状态”的页面。php。根据请求返回正确数据的逻辑在这里完成。
我创建了自定义重写规则,这样我就可以有一个带有自定义参数的漂亮URL,并将其发送到页面状态。php。以下代码位于我的主题函数中。php文件:
//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\' );
由于某种原因,重写规则无法坚持
sometimes (我讨厌事情有时才起作用)。我访问设置->永久链接刷新重写规则,一切都很好。几天过去了,我收到一封邮件,上面写着
http://www.pewhispanic.org/states/state/md/ 正在返回404。废话。我去刷新重写规则,一切又好了。
我错过了什么?为什么有时不应用我的自定义重写规则?有没有比generate_rewrite_rules
要抓住什么?我真的需要和redirect_canonical
滤器我不认为这有什么作用。
任何见解都将不胜感激。