Why matches[2] does not return anything in rewrite

2019-09-19 09:41发布

问题:

I'm trying to redirect URL www.domain.com/some/my-page--woohoo.html to www.domain.com/index.php?pagename=my-page

I have this rule that is attached to init via add_action('init', 'create_rewrite_rule', 1, 0);

add_rewrite_rule('^.*\/(.*)--woohoo\.html$', 'index.php?pagename=$matches[1]', 'top');

Calling www.domain.com/some/my-page--woohoo.html returns 404, but when is rule changed to

add_rewrite_rule('^.*\/(.*)--woohoo\.html$', 'index.php?pagename=my-page', 'top');

everything works.

It seems that $matches[1] returns nothing :-( Why?

Thank you for any help!

回答1:

If anybody will ever deal with this, I haven't found real solution (I tried MANY things for hours) so I had to deal with it thru regular htaccess redirects, even though it is not optimal (in my case).

If you found a solution, please write it here, otherwise I consider it as a bug in WP.