I am rewriting a Wordpress site and plan to turn part of it into an angular app.

The site has a bunch of sub-pages of the form /recommendations/xxxxx, and need to maintain these URLs for SEO purposes. But I want all these routes to return the /recommendations parent page whie keeping the rest of the URL present for Angular to process.

I think I need to use add_rewrite_rule but have got stuck, and the following does not work. I need wordpress to show the recommendations page even if there is an extra url-part subsequently.

function recommendations_rewrite() {
  add_rewrite_rule('^recommendations\/(.*)\/?', 'recommendations/', 'top');
add_action('init', 'recommendations_rewrite');

That said, this may be entirely wrong as I fear this will end up leading to URL rewrites, which is not what I want.

  • This question on StackOverflow may help you.
    – cybmeta
    Jul 4, 2015 at 7:19
  • I realise now that .htaccess cannot be the way forward and have rewritten the question to focus on wordpress hooks. Sorry for the confusion
    – Simon H
    Jul 4, 2015 at 8:08

1 Answer 1


No, you are on the right track as far as the add_rewrite_rule function is concerned. The generated rewrite rule doesn't send a 303 header.

The problem, in this case, is with your code which can be fixed easily.

On this line:

add_rewrite_rule('^recommendations\/(.*)\/?', 'recommendations/', 'top');

you are making WordPress to output a modified, custom .htaccess file with a nasty rewrite rule like this:

RewriteRule ^^recommendations\/(.*)\/? /recommendations/ [QSA,L]

This not only is ugly RegEx (you get a double beginning of line marker -- ^^) but the generated rule is broken because instead of the second ^ you should have /.

The solution is simple and it requires to locate the ID of the recommendations page / post which you would then use in the add_rewrite_rule function like so:

add_rewrite_rule( '^recommendations\/(.*)\/?', "index.php?page_id={$page_id}", 'top' );

where the {$page_id} variable statement in the example should be the said ID. By the way, the add_rewrite_rule function is presented on the WordPress Codex page.

After flushing the rewrite rules WordPress would serve an URL like:


with the content of

  • Hm, for some reason this doesn't work for me. I am using ` "index.php?page_id={2}"` and I know that page with id 2 exists and the action is executed, what can be the reason ?
    – gdfgdfg
    Feb 13, 2019 at 18:18
  • @gdfgdfg you need to write "index.php?page_id=2" - remove the {} brackets then it should work
    – Adrian
    Oct 9, 2019 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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