Search code examples
wordpressurlurl-rewritingpermalinks

Make WordPress show the same page on two different url patterns


I need to make WordPress show the same page on two different URLs:

https://sitename.com/%postname%/
https://sitename.com/%postname%/tail/

* I can only use functions.php


Solution

  • I used a few similar cases and guides and here is code which worked out for me:

    function tail_rewrite()
    {
        add_rewrite_rule('^([^/]*)/tail?', 'index.php?name=$matches[1]', 'top');
        // first parameter for posts: p or name, for pages: page_id or pagename
    }
    
    add_action('init', 'tail_rewrite');
    

    don't forget to flush the rules by visiting Settings > Permalinks OR use flush_rewrite_rules() in the plugin activation (don't execute it at every page load).