2

I am using $wp_rewrite->non_wp_rules

I have the following code that works:

'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
  • is there a reason why you're using non wp rules and not adding them to the internal rewrites? – Milo Apr 5 '14 at 3:26
  • I need it to be masked, and still be able to access the url I'm reference to. – Alex Pena Apr 5 '14 at 3:54
  • are you not using pretty permalinks? that's what the internal rewrites do if you've enabled pretty permalinks. – Milo Apr 5 '14 at 3:58
  • Yes, im using pretty permalinks, i have no problem rewriting for posts, just on pages. In posts I can do so something like sitedomain/slug/postname and still access it by going to sitedomain/postname. I want to be able to do the same but with page names instead of posts. Being able to access both urls, the one with base-slug and without. – Alex Pena Apr 5 '14 at 4:07
0

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.

|improve this answer|||||
  • It didn't work for the external rules, but this answer worked for what I was trying to do. So I'll stick with this one. Thanks! – Alex Pena Apr 5 '14 at 4:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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