add_rewrite_rule: $matches var not replaced by captured value

That rewrite rule won’t work at all, flushed or not.

There’s two kinds of rewrite rules in WordPress. Those that have their rewrite urls start with index.php and those that don’t.

All WordPress internal rewrite rules must start with index.php. It can’t redirect to anyplace else, because it’s already been redirected to the index.php file by the time the WordPress functions are processing the ruleset. So non-index.php rules have to go into the .htaccess directly, so that they can be redirected to anywhere other than index.php.

  • Those that start with index.php will be processed inside WordPress itself, and the $matches[1] syntax will be the correct one to use in that case.

  • Those that don’t start with index.php will instead be written to the .htaccess file as direct RewriteRules. The $matches[1] syntax makes no sense in that case.

If you look in your .htaccess file after using your rule, you’ll find this:

RewriteRule continue/([0-9]+)/?$ wp-content/plugins/my-plugin/continue.php?where=$matches[1]

Which obviously won’t work.

Instead, try making your code into this:

add_rewrite_rule( 'continue/([0-9]+)/?$', 'wp-content/plugins/my-plugin/continue.php?where=$1');

Note the $1 replacement instead? This will work properly when converted to a normal RewriteRule.

Leave a Comment