Redirect sub-pages to parent without changing URL

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:

http://www.example.com/recommendations/this/is/my/recommendation/

with the content of

http://www.example.com/recommendations/

Leave a Comment