Pages should have priority when using add_rewrite_rule

I’m using the following code to have dynamic pages

function custom_rewrite_basic() {
  add_rewrite_rule('^prefix-(.*)', 'foo/bar/index.php?page=$matces[1]', 'top');
}
add_action('init', 'custom_rewrite_basic');

This catches /prefix-* pages and serves my index.php file. For example: /prefix-foo serves foo/bar/index.php?page=foo.

If I add a new page with the url /prefix-foo, I want it to have priority (to serve the content from the database, not from my PHP file).

How can I do that without changing the regex?

1 Answer
1

If I understand you correctly, you could try to replace:

add_rewrite_rule( '^prefix-(.*)', 'foo/bar/index.php?page=$matces[1]', 'top' );

with

add_rewrite_rule( '^prefix-(.*)', 'foo/bar/index.php?page=$matces[1]', 'bottom' );

From the Codex:

top‘ will
take precedence over WordPress’s existing rules, where ‘bottom‘ will
check all other rules match first. Default: “bottom

Leave a Comment