0

I would like every URL of my WordPress site that looks like /fr/something to show exactly the same page as /something, but without redirecting; I want to keep the URL.

I think I'm close, I used :

add_filter( 'rewrite_rules_array', 'my_insert_rewrite_rules' );
add_filter( 'query_vars', 'my_insert_query_vars' );
add_action( 'init', 'my_flush_rules' );

// flush_rules() if our rules are not yet included
function my_flush_rules() {
    $rules = get_option( 'rewrite_rules' );        
    if ( ! isset( $rules['fr(\/(.*))?$'] ) ) {
        global $wp_rewrite;
        $wp_rewrite->flush_rules();
    }
}        
// Adding a new rule
function my_insert_rewrite_rules( $rules ) {
    $newrules = array();
    $newrules['fr(\/(.*))?$'] = 'index.php?pagename=$matches[1]';
    return $newrules + $rules;
}        
// Adding the id var so that WP recognizes it
function my_insert_query_vars( $vars ) {
    array_push( $vars, 'id' );
    return $vars;
}

It works on some URLs but not all :

  • /fr shows the same as / => Fine
  • /fr/page-d-example shows the same as /page-d-example => Fine
  • /fr/2015/09/25/bonjour-tout-le-monde/ redirects to /2015/09/25/bonjour-tout-le-monde/ => Not fine

Do you know why the last one is redirecting and how to prevent that?

  • 1
    In your rewrite_rules_array filter you can look at $rules and see everything you'd need to duplicate. There are a lot of them- single posts, author archives, date archives, taxonomy archives, post formats, attachments, plus pagination and feeds for all of them. – Milo Oct 18 '15 at 20:59
  • Ok that worked, I ended up with : function my_insert_rewrite_rules( $rules ) { $newrules = array(); foreach($rules as $k => $r) { $newrules['fr/'.$k] = $r; } $newrules['fr/?'] = '/index.php'; return $newrules + $rules; } I had to manually add the last rule for just 'fr/' url – Mansur Khan Oct 18 '15 at 21:18
  • 1
    Glad you solved it! Please add your final code as an answer and accept it so it may help future visitors, thanks! – Milo Oct 19 '15 at 1:21
0

Milo's advice set Mansur Khan in the right direction:

In your rewrite_rules_array filter you can look at $rules and see everything you'd need to duplicate. There are a lot of them- single posts, author archives, date archives, taxonomy archives, post formats, attachments, plus pagination and feeds for all of them.

The updated, working code:

add_filter( 'rewrite_rules_array', 'my_insert_rewrite_rules' );
add_filter( 'query_vars', 'my_insert_query_vars' );
add_action( 'init', 'my_flush_rules' );

// flush_rules() if our rules are not yet included
function my_flush_rules() {
    $rules = get_option( 'rewrite_rules' );        
    if ( ! isset( $rules['fr(\/(.*))?$'] ) ) {
        global $wp_rewrite;
        $wp_rewrite->flush_rules();
    }
}   

// Adding a new rule
function my_insert_rewrite_rules( $rules ) {
  $newrules = array();
  foreach( $rules as $k => $r ) {
      $newrules['fr/'.$k] = $r;
  }

  $newrules['fr/?'] = '/index.php';

  return $newrules + $rules;
}

// Adding the id var so that WP recognizes it
function my_insert_query_vars( $vars ) {
    array_push( $vars, 'id' );
    return $vars;
}

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.