I'm having some issue rewriting a page URL with wordpress. I tried with regular apache mod_rewrite, but the result was inconclusive. So I chose to focus on wordpress rewriting
my dev domain is:
I'm trying to rewrite this adress
/index.php?p=13806&annee=2014
as
/resultats/2014/
where resultats is p=13806 and 2014 is 'annee' (year in french)
my functions.php:
function add_result_url() {
add_rewrite_rule('^resultats/([0-9]+)/?', 'index.php?p=13806&annee=$matches[1]', 'top');
add_rewrite_tag('%annee%','([0-9]+)');
};
add_action('init', 'add_result_url');
flush_rewrite_rules();
my template file
global $wp_query;
$season = $wp_query->query_vars['annee'];
var_dump ($wp_query->query_vars['annee']);
the var_dump return a null on
/resultats/2014/
but works on
resultats/?annee=2014
/index.php?p=13806&annee=2014 (wich redirect on the first one)
Thanks for your help
EDIT 1
I check the 2 link from ihor's answear but couldn't get it worked with:
functions.php
function add_custom_query_var( $vars ){
$vars[] = "annee";
return $vars;
}
add_filter( 'query_vars', 'add_custom_query_var' );
function add_rewrite_rules($aRules) {
//add_rewrite_rule('^resultats/([^/]*)/([^/]*)/$', '?p=13806&annee=$matches[1]&gp=$matches[2]', 'bottom');
$aNewRules = array('^resultats/([0-9]*)/$' => 'index.php?p=13806&annee=$matches[1]');
$aRules = $aNewRules + $aRules;
return $aRules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');
and
template file:
if(isset($wp_query->query_vars['annee'])) {
$season = urldecode($wp_query->query_vars['annee']);
}
var_dump ($season);
var_dump ($_GET["annee"]);