I would like to add a rewrite for only certain pages (not all pages) that would insert a base slug into the permalink. This is sort of a unique situation where we know all the page names beforehand and they will not change.
For example:
sitename.com/pagetitle
to
sitename.com/service/pagetitle
I am trying to use "add_rewrite_rule", but not having much luck. Below is one of my iterations and I have had many. Any guidance would be appreciated.
I understand that there is a plugin that allows you to totally change your permalinks of your post and pages to whatever you want, but was hoping I could do it programmatically myself.
add_action( 'init', 'page_change' );
function page_change()
{
add_rewrite_rule(
'^pagenamehere/?',
'index.php?post_type=service',
'top' );
}
Updated code below:
function new_rewrite_rule()
{
add_rewrite_rule('^services/statictitle$', 'index.php?pagename=oldname', 'top');
}
add_action('init', 'new_rewrite_rule');
So this code allows the link to be accessible at 'services/statictitle.' However, the old link is still accessible as well. So I am not sure I am going about this in the correct way.....Any help would be appreciated.
Thanks.
Update
Here is an alternative way that I think might be more suitable. Although, this targets all pages. Would there be a way to just target the pagename or id?
function custom_page_rules() {
global $wp_rewrite;
$wp_rewrite->page_structure = $wp_rewrite->root . 'services/%pagename%';
}
service
page and making pages children of that page?