- Status
- Resolved
- Resolved in
- 3.7.0
Overview of the issue
Since WPML 3.6.3 some users are experiencing incorrect browser redirections when the Language URL format is configured on WPML -> Languages page with the following settings:
- Different languages in directories
- Use directory for default language => enabled
Workaround
This problem will be addressed in the next WPML version.
In the meantime we have a patch you can apply to the class-wpml-canonicals-hooks.php file, found in the ../wp-content/plugins/sitepress-multilingual-cms/classes/canonicals/ folder.
Replace the following whole block of code, around line 34.:
public function redirect_pages_from_root_to_default_lang_dir() {
if ( is_page() ) {
$current_path = wpml_parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
$canonical_url = get_permalink( get_queried_object_id() );
$canonical_path = wpml_parse_url( $canonical_url, PHP_URL_PATH );
$canonical_path = $this->get_paginated_canonical_path( $canonical_path );
if ( $current_path !== $canonical_path ) {
$current_query = wpml_parse_url( $_SERVER['REQUEST_URI'], PHP_URL_QUERY );
parse_str( $current_query, $current_query_array );
$canonical_url = add_query_arg( $current_query_array, $canonical_url );
$this->sitepress->get_wp_api()->wp_safe_redirect( $canonical_url, 301 );
}
}
}
With:
public function redirect_pages_from_root_to_default_lang_dir() {
global $wpml_url_converter;
if ( is_page() && ! WPML_Root_Page::is_current_request_root() ) {
$lang = $this->sitepress->get_current_language();
$current_uri = $_SERVER['REQUEST_URI'];
$abs_home = $wpml_url_converter->get_abs_home();
$install_subdir = wpml_parse_url( $abs_home, PHP_URL_PATH );
$actual_uri = preg_replace( '#^' . $install_subdir . '#', '', $current_uri );
$actual_uri = '/' . ltrim( $actual_uri, '/' );
if ( 0 !== strpos( $actual_uri, '/' . $lang . '/' ) ) {
$canonical_uri = trailingslashit( $install_subdir ) . $lang . $actual_uri;
$canonical_uri = user_trailingslashit( $canonical_uri );
$this->sitepress->get_wp_api()->wp_safe_redirect( $canonical_uri, 301 );
}
}
}