2

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?

4

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"

4
  • Hmm, sorry, but this doesn't work, I have just tested. After adding the rule, the page was working (e.g. /foo) and then I created a WP page with the same url (/foo). After creating the page, the url is still fetching my PHP file. Why? – Ionică Bizău Mar 14 '16 at 4:21
  • Thanks for the status update. The add_rewrite_rule() is a wrapper for WP_Rewrite::add_rule(). There we see that if these are external rules it uses WP_Rewrite::add_external_rule() instead that only works for Apache and not e.g. NginX. Note the descriptions here where it says: @IonicăBizău – birgire Mar 14 '16 at 8:29
  • "Will add the non_wp_rules property rules to the .htaccess file before the WordPress rewrite rules one." There's a mod_rewrite_rules filter here that gives you the possibility to adjust the generated rules before it's stored in the .htaccess file, but I don't see how it would work to move them after the WordPress rewrite rules. So you should probably use another workaround than the add_rewrite_rule() in this case. @IonicăBizău – birgire Mar 14 '16 at 8:34
  • I see that you've already got a great workaround by Milo ;-) @IonicăBizău – birgire Mar 14 '16 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.