$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>url rewriting - Help adding custom url, rewrite_rules_array|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

url rewriting - Help adding custom url, rewrite_rules_array

matteradmin7PV0评论

I'm trying to get a rewrite_rules_array rule to work and running into issues. I would like to add a rule so that when someone visits:

It essentially loads:

What really happens is that Wordpress redirects you to .

add_filter('rewrite_rules_array', function( $rules ) {
    $new = array();
    $new['product/(.*)/(.*)/?'] = 'index.php?pagename=$matches[1]&view=$matches[2]';
    return array_merge( $new, $rules );
});
  • I've flushed my permalinks.
  • view has been added to the query_vars.

I'm trying to get a rewrite_rules_array rule to work and running into issues. I would like to add a rule so that when someone visits:

http://domain/product/foo/bar

It essentially loads:

http://domain/product/foo?view=bar

What really happens is that Wordpress redirects you to http://domain/product/foo.

add_filter('rewrite_rules_array', function( $rules ) {
    $new = array();
    $new['product/(.*)/(.*)/?'] = 'index.php?pagename=$matches[1]&view=$matches[2]';
    return array_merge( $new, $rules );
});
  • I've flushed my permalinks.
  • view has been added to the query_vars.
Share Improve this question asked Nov 14, 2018 at 22:20 Louis WLouis W 4741 gold badge6 silver badges18 bronze badges 2
  • Your products are pages of the page post type? Also- just use add_rewrite_rule hooked to init if you’re just adding rules and not manipulating existing rules or reordering. – Milo Commented Nov 14, 2018 at 23:00
  • @Milo They are of type 'product'. – Louis W Commented Nov 15, 2018 at 2:19
Add a comment  | 

1 Answer 1

Reset to default 0

For anyone looking, the solution was:

add_action('init', function() {

     add_rewrite_rule(
        'product/(.*)/(.*)/?',
        'index.php?product=$matches[1]&view=$matches[2]',
        'top'
    );

});
Post a comment

comment list (0)

  1. No comments so far