- Status
- Resolved
Overview of the issue
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 );