I need to setup a WP rewrite structure in which I prefix all urls with a custom (dynamic) prefix.
So the urls would look like this:
www.example.com/myprefix1/
www.example.com/myprefix2/
www.example.com/myprefix1/a-page
www.example.com/myprefix1/a-category
www.example.com/myprefix1/a-cpt
www.example.com/myprefix2/a-cpt
etc.
So far I came up with this:
$options = implode( array('myprefix1','myprefix2', '|' ); // my prefix options
// add a rule for each registered post type (??)
foreach($postTypes as $postType){
$new_rules['(' . $options . ')/(.+?)/?$'] = 'index.php?post_type=".$postType."&' . self::$sitePrefixName . '='. $wp_rewrite->preg_index(1).'&pagename=". $wp_rewrite->preg_index(2);
}
// add a rule for the home page (?)
$new_rules["(' . $options . ')/?$'] = 'index.php?' . self::$sitePrefixName . '='. $wp_rewrite->preg_index(1);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
It works, but I believe e.g. taxonomies are not included here. I’m trying to make this as flexible as possible so that future CPT’s, tax etc. will work as well.
Am I doing the right thing here or are there other ways to do this?
Thanx!