Resolved

Symptoms

When the URL is invalid in one language, WPML checks if this URL could be valid in another language. If this is confirmed, WPML will redirect the visitor to the correct URL with the language information.

However, this behavior might cause issues with custom rewrite rules.

Workaround

WPML provides the wpml_is_redirected filter on this redirection. The following is an example of how to use it:

/**
* This will prevent redirection in the secondary language for the custom rule:
* '^products/([A-Za-z0-9-]+)/?' => 'index.php?pagename=new-sample&type=$matches[1]'
*/
function wpmlcore_4704_block_redirect( $redirect, $post_id, $q ) {
// Decide on which condition we should prevent the redirection
if ( isset( $q->query_vars['pagename'] ) && 'sample-page' === $q->query_vars['pagename'] ) {
return false;
}

return $redirect;
}
add_filter( 'wpml_is_redirected', 'wpmlcore_4704_block_redirect', 10, 3 );

2 Responses to “WPML redirection and issues with custom rewrite rules”

    • Hi @washingplant,

      I am not sure if I understand your question. If you mean “Can I disable the WPML redirection on all my website pages?”, the answer is yes. Just remove the condition and always return `false`.


      function wpmlcore_4704_block_redirect( $redirect, $post_id, $q ) {
      return false;
      }

      But this NOT something I would recommend because it might cause other issues where the WPML redirection is really needed.