Reputation: 21
I'm trying to rewrite an URL
from: http://localhost/hockey-oefening/70/interceptie-warming-up
to: http://localhost/hockey-oefening/?oefening=70&naam=interceptie-warming-up
I tried to add manually .htaccess file. However, I did not get it to work. So I reset the .htaccess file and looked for another way.
Then I added the following code in my functions.php
function custom_rewrite_rule() {
add_rewrite_tag('%oefening%', '([^&]+)');
add_rewrite_tag('%naam%', '([^&]+)');
add_rewrite_rule('^hockey-oefening/([^/]*)/([^/]*)?', 'index.php?page_id=563&oefening=$matches[1]&naam=$matches[2]', 'top' );
}
add_action('init', 'custom_rewrite_rule', 10, 2);
This adds a rule to the $wp_rewrite array as I can see with
global $wp_rewrite;
print_r($wp_rewrite);
The output:
[extra_rules_top] => Array
(
[^wp-json/?$] => index.php?rest_route=/
[^wp-json/(.*)?] => index.php?rest_route=/$matches[1]
[^index.php/wp-json/?$] => index.php?rest_route=/
[^index.php/wp-json/(.*)?] => index.php?rest_route=/$matches[1]
[service/?$] => index.php?post_type=services
[service/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?post_type=services&feed=$matches[1]
[service/(feed|rdf|rss|rss2|atom)/?$] => index.php?post_type=services&feed=$matches[1]
[service/page/([0-9]{1,})/?$] => index.php?post_type=services&paged=$matches[1]
[^hockey-oefening/([^/]*)/([^/]*)?] => index.php?page_id=563&oefening=$matches[1]&naam=$matches[2]
)
Now when I try to go to http://localhost/hockey-oefening/70/interceptie-warming-up
, I get a redirection to http://localhost/registreren/
Now I was thinking that my page_id might be wrong, but the hockey-oefening page has id: 563 and the register page id: 11.
Upvotes: 0
Views: 1293
Reputation: 1020
Try this following code
add_filter('query_vars', 'parameter_queryvars' );
function parameter_queryvars( $qvars )
{
$qvars[] = 'oefening';
$qvars[] = 'naam';
return $qvars;
}
//rewrite part
function wpse120653_init() {
add_rewrite_rule(
'hockey-oefening(/([^/]+))?(/([^/]+))?/?',
'index.php?pagename=hockey-oefening&oefening=$matches[2]&naam=$matches[4]',
'top'
);
}
add_action( 'init', 'wpse120653_init' );
Hope it works
Upvotes: 0