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:








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?



Have you ever consider to use rewrire_rules_array filter to alter $rewrite_rules?

 * Example of Changing All Rewrite Rules
 * @param  array $rewrite_rules The compiled array of rewrite rules.
 * @return array                The compiled array of rewrite rules (altered).
function wpse_263624_rewrite_rules_example( $rewrite_rules ) {

    $prefixes = array( 'otro', 'esto' );
    $prefix_regexp = '(' . implode( '|', $prefixes ) . ')/';
    $new_rewrite_rules = array();

    $match_pattern = '/\$matches\[(\d)\]/si';

    foreach ( $rewrite_rules as $rewrite_rule => $match ) {

        // Incrementing $match becaouse prefixes goes as capturing group.
        $new_match = preg_replace_callback( $match_pattern , function( $m ){
            return sprintf('$matches[%d]', $m[1] + 1);
        }, $match);

        // Adding Prefix Group and New Value for our match.
        $new_rewrite_rules[ $prefix_regexp . $rewrite_rule ] = $new_match;
    return $new_rewrite_rules;
add_filter( 'rewrite_rules_array', 'wpse_263624_rewrite_rules_example' );

It ends up in something like this..

changed rewrite rules array

  • Hi Oleg, thanx for your reply. Your approach makes sense, but I would need the prefix as an addition parameter (wp query var). This is filtered out right? – john23klipp Apr 25 '17 at 15:58
  • What stops you from changing my example so it fits your needs? – Butuzov Apr 25 '17 at 16:32
  • right now my regex knowledge... :) but I'm trying to figure it out – john23klipp Apr 25 '17 at 17:28
  • OK. Checkout what filter query_vars does (hint it will add your prefix query_var). Next Check my code, you need just to add your prefix and $match[1] to rule ($new_match). You also can use my tool (debug bar plugin) for debugging rewrite rules wordpress.org/plugins/debug-bar-rewrite-rules . Sorry but I can't provide more detailed answer due a lack of time. – Butuzov Apr 25 '17 at 18:07

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.