url rewriting - Help adding custom url, rewrite_rules_array

admin2025-06-06  7

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'
    );

});
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749180867a317038.html

最新回复(0)