Rewrite Page with a slug before page name

I am using $wp_rewrite->non_wp_rules

I have the following code that works:

Read More
'name/(.*)' => 'index.php?post_type=post';

Im able to use a url like http://mysite.com/custom_name/post_name

using $wp_rewrite->non_wp_rules, how can I make pages work the same way.

This is just an example:

'anothername/(.*)' => 'index.php?post_type=page'; //for all pages

'anothername/(.*)' => 'page/pagename' //for one specific page

Is there a way I can have a page look like this:

 http://mysite/custom_name/page_name.

The two examples above just redirects me to:

http://mysite/page_name  //without the slug

Related posts

Leave a Reply

1 comment

  1. I’ve not tried external rules, but you can achieve that with an internal rewrite and pagename:

    function wpd_page_rewrite(){
        add_rewrite_rule(
            '^anothername/([^/]*)/?',
            'index.php?pagename=$matches[1]',
            'top'
        );
    }
    add_action( 'init', 'wpd_page_rewrite' );
    

    For parent/child pages you have to set pagename to the parent/child path, page/pagename, or you can also use page_id and use the page’s ID.

    EDIT – note that you have to flush rewrite rules after adding new ones. a quick way to do this during testing is to visit the permalinks settings page.